【LeeCode】142. Circular linked list II

Given the head node of a linked list head , return the first node where the linked list begins to enter the loop. If the linked list has no cycle, return null.

If there is a node in the linked list that can be reached again by continuously tracking the next pointer, then there is a cycle in the linked list. In order to represent a ring in a given linked list, the evaluation system internally uses an integer pos to represent the position where the tail of the linked list is connected to the linked list (The index starts from 0, just to identify the actual situation of the linked list. is not passed as a parameter Note: , then there is no cycle in the linked list. pos is ). If -1pos

Modification is not allowed linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: Return the linked list node with index 1
Explanation: There is a ring in the linked list, the tail of which is connected to the second node. 

Example 2:

Input: head = [1,2], pos = 0
Output: Return the linked list node with index 0
Explanation: There is a ring in the linked list, the tail of which is connected to the first node. 

Example 3:

Input: head = [1], pos = -1
Output: return null
Explanation: There is no cycle in the linked list. 

This question not only examines operations on linked lists, but also requires some mathematical operations.

Mainly examine two knowledge points:

  • Determine whether the linked list is a cycle

  • If there is a ring, how to find the entrance of this ring?

Determine whether the linked list has a cycle

You can use the fast and slow pointer method to define fast and slow pointers respectively. Starting from the head node, the fast pointer moves two nodes at a time, and the slow pointer moves one node at a time. If the fast and slow pointers meet on the way, it means that the linked list has a cycle.

Why does fast go to two nodes and slow goes to one node? If there is a ring, they will definitely meet in the ring instead of being staggered forever.

First point:The fast pointer must enter the ring first. If the fast pointer and the slow pointer meet, they must meet in the ring. This is beyond doubt.

Let’s take a look,Why do the fast pointer and the slow pointer definitely meet?

You can draw a ring, and then let the fast pointer start catching up with the slow pointer at any node.

You will find that this is the case in the end, as shown below:

Fast and slow each take one more step, and fast and slow meet.

This is because fast takes two steps and slow takes one step.In fact, compared to slow, fast is closer to slow one node at a time< a i=2>, so fast can definitely overlap with slow.

If there is a ring, how to find the entrance of this ring?

At this point it can be determined whether the linked list has a ring, then the next step is to find the entrance to the ring.

Assume that the number of nodes from the head node to the ring entrance node is x. The number of nodes from the ring entry node to the node where the fast pointer and the slow pointer meet is y. The number of nodes from the encounter node to the ring entrance node is z.​ 

Then when they meet: The number of nodes passed by the slow pointer is: x + y, the number of nodes passed by the fast pointer: x + y + n (y + z), n is the fast pointer After walking n times in the ring, the slow pointer is encountered. (y+z) is the number A of nodes in the ring.

Because the fast pointer walks two nodes in one step, and the slow pointer walks one node in one step, the number of nodes traveled by the fast pointer = the number of nodes traveled by the slow pointer * 2:

(x + y) * 2 = x + y + n (y + z)

Eliminate one (x+y) on both sides:x + y = n (y + z)

Because you want to find the entrance of the ring, what is required is x, because x represents the distance from the head node to the ring entrance node.

So to require x, place x alone on the left:x = n (y + z) - y,

Then propose a (y+z) from n(y+z). After sorting out the formula, it becomes the following formula:x = (n - 1) (y + z) + z Note that n must be greater than or equal to 1, because The fast pointer must travel at least one more circle to meet the slow pointer.

What does this formula mean?

Let's take the case where n is 1 as an example. This means that after the fast pointer makes one turn in the ring, it encounters the slow pointer.

When n is 1, the formula resolves to x = z,

This means,A pointer starts from the head node and a pointer starts from the meeting node. These two pointers only go to one node at a time. Then when these two When the pointers meet, it is the node of the ring entrance.

That is, define a pointer index1 at the encounter node, and define a pointer index2 at the head node.

Let index1 and index2 move at the same time, one node at a time, then the place where they meet is the node at the ring entrance.

So what happens if n is greater than 1? It means that the fast pointer only encounters the slow pointer after n circles.

In fact, the effect of this situation is the same as when n is 1. You can also find the entry node of the ring through this method. However, the index1 pointer rotates (n-1) more times in the ring, and then encounters index2 again. The meeting point is still the entry node of the ring.

untie:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {// 有环
                ListNode index1 = fast;
                ListNode index2 = head;
                // 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
                while (index1 != index2) {
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}

Guess you like

Origin blog.csdn.net/weixin_48144018/article/details/134773009