Question
Formatted question description: https://leetcode.ca/all/142.html
142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
@taglinkedlist
Algorithm
Fast and slow pointers, but this time you want to record where the two pointers meet. When the two pointers meet, let one of the pointers start from the head of the linked list, then start moving again, and the position where they meet again is the starting position of the ring in the linked list.
Because the fast pointer moves 2 each time and the slow pointer moves 1 each time, the fast pointer travels twice as far as the slow pointer. And the fast pointer goes one more circle than the slow pointer.
So the distance from head to the starting point of the ring, plus, the starting point of the ring to the point where they meet
, is equal to the distance of one circle of the ring
. Now start again, the distance from head to the starting point of the ring and the distance from the meeting point to the starting point of the ring is also equal, which is equivalent to subtracting the distance from the beginning of the ring to the point where they meet.
a b
A  B +
 
c  
C +
* A: 起始点
* B: Cycle Begins
* C: 1st 快慢指针相遇点
* A>B: a
* B>C: b
* C>B: c
* 环的长度 (b+c) 为 R
To understand this solution, you just need to ask yourself these question.
Assume the distance from head to the start of the loop is a
the distance from the start of the loop to the point fast and slow meet is b
the distance from the point fast and slow meet to the start of the loop is c
 What is the distance fast moved?
 What is the distance slow moved?
 And their relationship?
fast: a + b + c + b
slow: a + b
relationship  fast is 2X faster than slow: a + b + c + b = 2 * (a + b)
Thus a = c
Code
Java

public class Linked_List_Cycle_II { /** * Definition for singlylinked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { /* To understand this solution, you just need to ask yourself these question. Assume the distance from head to the start of the loop is x1 the distance from the start of the loop to the point fast and slow meet is x2 the distance from the point fast and slow meet to the start of the loop is x3 What is the distance fast moved? What is the distance slow moved? And their relationship? x1 + x2 + x3 + x2 x1 + x2 x1 + x2 + x3 + x2 = 2 (x1 + x2) Thus x1 = x3 Finally got the idea. */ public ListNode detectCycle(ListNode head) { if (head == null) return head; ListNode slow = head, fast = head; boolean circle = false; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; // if (fast == slow) if (fast == slow) { circle = true; break; } } if (circle) { fast = head; while (fast != slow) { fast = fast.next; slow = slow.next; } return fast; } return null; } } }

// OJ: https://leetcode.com/problems/linkedlistcycleii/ // Time: O(N) // Space: O(1) class Solution { public: ListNode *detectCycle(ListNode *head) { if (!head) return nullptr; auto p = head, q = head; while (p && p>next) { p = p>next>next; q = q>next; if (p == q) break; } if (!p  !p>next) return nullptr; // The list is finite p = head; for (; p != q; p = p>next, q = q>next); return p; } };

# Definition for singlylinked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ slow = fast = finder = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: while finder != slow: finder = finder.next slow = slow.next return finder return None