Linked List Cycle II

Floyd's tortoise & hare — once they meet, restart one pointer from head; both step at speed 1 and converge at the cycle entry.

Medium
Time O(n) Space O(1)
LeetCode
3 min read
linked-list two-pointers cycle-detection

Problem#

Return the node where the cycle begins in a linked list, or null if none.

Examples#

  • [3,2,0,-4] with cycle to index 1 → node with value 2.
  • [1,2] with cycle to index 0 → node with value 1; [1] no cycle → null.

Constraints#

  • 0 <= nodes <= 10^4.

Approach#

Floyd’s algorithm. Phase 1: slow steps 1, fast steps 2; they meet inside the cycle iff one exists. Phase 2: reset slow to head; both step 1 at a time; they meet at the cycle entry.

Solution#

Linked List Cycle II
struct ListNode { int val; ListNode* next; };
class Solution {
public:
ListNode* detectCycle(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
ListNode* p = head;
while (p != slow) { p = p->next; slow = slow->next; }
return p;
}
}
return nullptr;
}
};

Editorial#

Algebraically: if the head-to-cycle-start distance is a and the meeting point is b steps into the cycle, then 2 (a + b) = a + b + k * C for some integer k, so a = k * C - b. Walking a steps from head and a steps from the meeting point converges at the cycle entry.

Complexity#

  • Time: O(n).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.