Linked List Cycle II medium
Description
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Do not modify the linked list.
Examples
> Case 1:
list = [3, 2, 0, -4], cycle starts at index 1
Output: node with value 2
> Case 2:
list = [1, 2], cycle starts at index 0
Output: node with value 1
> Case 3:
list = [1], no cycle
Output: nullConstraints
- The number of nodes is in the range
[0, 10^4]. -10^5 <= Node.val <= 10^5.posis-1(no cycle) or a valid index in the linked list.
Required: O(1) extra space.
State design
A two-phase fast-and-slow algorithm.
Phase 1: Detect the cycle.
slowmoves 1 step at a time.fastmoves 2 steps.- If
fastruns off the end → no cycle, returnnull. - If
slow == fast→ cycle exists. Note the meeting point.
Phase 2: Find the cycle’s entry.
- Reset one pointer (say
slow) tohead. Leavefastat the meeting point. - Advance both at the same speed (1 step each).
- Where they meet IS the cycle’s entry.
The math justifying phase 2 is in the Fast and Slow page.
Code
The proof in one paragraph: Let L = distance from head to cycle entry, C = cycle length, m = distance from cycle entry to the first meeting point. When they meet, slow has walked L + m and fast has walked 2(L + m) = L + m + kC for some integer k (it lapped the cycle k extra times). Solving: L + m = kC, so L = kC − m. Walking L steps from the head AND L steps from the meeting point both land at the cycle entry. Walk both at speed 1 and they meet there.
Why the hash-set approach is rejected
The hash-set version stores every visited node and checks each new one. O(n) time, O(n) space. The constraint “O(1) extra space” forces the Floyd’s trick.
Analysis
- Time:
O(n)total. Phase 1 finishes in at mostnsteps; phase 2 in at mostnmore. - Space:
O(1).
Same skin
- Linked List Cycle (the easy version) — only detect, don’t find the entry. Phase 1 alone.
- Find the Duplicate Number — model array indices as a linked list, then run this exact two-phase algorithm.
- Happy Number — Floyd’s on the “sum of squared digits” function, detecting a cycle in number-iteration space.
- Middle of the Linked List — same fast/slow setup, return slow when fast reaches the end.
This is the single most-asked fast/slow problem in interviews. Knowing both the algorithm AND the proof is the difference between rote memorization and demonstrating understanding.