(Java refers to offer) The entry node of the ring in the linked list

1. Question analysis

For a linked list, if it contains a ring, please find the entry node of the ring of the linked list, otherwise, output null.

Setting the fast and slow pointers starts from the linked list. The fast pointer moves two steps at a time and the slow pointer moves one step at a time. If there is a ring, it will meet at a certain point in the ring.

Conclusion 1: The two pointers continue from the head of the linked list and the meeting point, and each time they take a step, they must meet and the ring entrance at the end.
Proof conclusion 1: Set the fast and slow pointers fast and low, fast takes two steps each time, and low takes one step each time. If there is a ring, the two will definitely meet (because once low enters the ring, it can be seen as a process in which fast catches up with low. Each time the two are close to one step, they will definitely catch up).

Then let the two pointers start from the meeting point and the head of the linked list respectively, and both are changed to take one step at a time, and finally meet at the ring entrance

Conclusion 2: The two pointers start from the meeting point and the head of the linked list respectively, and both are changed to take one step at a time, and finally meet at the
ring entrance. The length from the head of the linked list to the ring entrance is: the length from the
ring entrance to the meeting point is: b
meeting point Length to ring entrance: c
Insert picture description here
Insert picture description here

Second, the code

/**
 * @Auther: Yolo
 * @Date: 2020/9/14 08:56
 * @Description:
 */


public class Test_13 {
    
    
    public static void main(String[] args) {
    
    
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node3;
        ListNode node = EntryNodeOfLoop(node1);
        System.out.println(node.val);
    }

    private static ListNode EntryNodeOfLoop(ListNode pHead) {
    
    
        ListNode fastNode=pHead;
        ListNode lowNode=pHead;
        //找到相遇点
        while (fastNode != null && fastNode.next != null) {
    
    
            fastNode=fastNode.next.next;
            lowNode=lowNode.next;
            if (fastNode == lowNode) {
    
    
                break;
            }
        }
        if (fastNode == null || fastNode.next == null) {
    
    
            return null;
        }
        //将慢节点重置为从头结点出发
        lowNode=pHead;
        //查找环入口结点
        while (fastNode!=lowNode){
    
    
            fastNode=fastNode.next;
            lowNode=lowNode.next;
        }
        return lowNode;
    }
}

Three, summary

This problem is not a simple routine to form a ring from the beginning node, but a ring may be formed from a node.

Guess you like

Origin blog.csdn.net/nanhuaibeian/article/details/108573480