Linked List Cycle II

Find the node where a cycle begins using Floyd's two-phase detection.

Medium
Time O(n) Space O(1)
LeetCode
3 min read
fast-slow linked-list

Problem#

Return the node where a cycle in the linked list begins, or nullptr if no cycle exists.

Examples#

  • [3,2,0,-4] with tail → index 1 → node with value 2.
  • [1, 2] no cycle → nullptr.

Constraints#

  • Length in [0, 10^4].

Hints#

Hint 1
After tortoise-hare meet inside the cycle, distance from list-head to cycle-start equals distance from meeting-point to cycle-start (mod cycle length).

Approach#

Phase 1: tortoise/hare meet inside the cycle (if any). Phase 2: reset one pointer to head; advance both at speed 1; they meet at the cycle’s entry.

Solution#

Linked List Cycle II
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#

Let L = head-to-cycle distance, C = cycle length, D = distance from cycle-entry to meeting point. At meet: slow has walked L + D, fast L + D + kC for some k ≥ 1. From 2(L+D) = L + D + kC we get L = kC - D, i.e., walking L steps from the meeting point lands at the cycle entry. The second loop exploits this exactly.

Complexity#

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

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.