Linked List Cycle II solution explained
To find where a linked list's cycle starts, run Floyd's algorithm: a slow pointer moves one step and a fast pointer two until they meet inside the loop. Then start one pointer at the head and keep the other at the meeting point, move both one step at a time, and the node where they meet is the start of the cycle. The Linked List Cycle II solution runs in O(n) time with O(1) extra space and does not change the list.
By Evin JohnPublished 6 min read
The task, restated
A singly linked list may end in a loop: some node's next pointer leads back to an earlier node. Given the head, return the node where the loop begins, meaning the first node you would visit twice. If the list reaches null, return null. You may not modify the list, and the follow-up asks for constant extra memory.
| List | Last node points to | Answer |
|---|---|---|
| 6 → 2 → 8 → 5 → 9 | the node holding 8 | The node holding 8 |
| 7 → 1 | the head (7) | The head: the whole list is the loop |
| 4 | itself | The head: a self-loop |
| 3 → 3 → 3 | the second node | The second node, not the first, even though values match |
| 1 → 2 → 3 | null | null: no cycle |
As with any pointer problem, the answer is a node, not a value. The fourth row shows why: all three nodes hold 3, and only the identity of the node tells you where the loop starts.
How interviewers use it
The first part, detecting that a cycle exists, is a common warm-up. This version asks for the entry node, and the interesting part is the second phase of Floyd's algorithm and the reason it works. Interviewers often accept the code and then ask, "Why does resetting to the head find the entry?" A clear answer to that is what separates a memorized solution from an understood one.
- Return the loop length. After the meeting, hold one pointer still and walk the other around until it returns, counting steps.
- Return the length of the part before the loop. That is the number of steps in phase two.
- Find a duplicate in an array of n + 1 values from 1 to n. Treat each value as a next pointer; the duplicate is the cycle entry.
- Detect the loop with Brent's algorithm. It finds the loop length directly and often makes fewer pointer moves.
Brute force: remember visited nodes
def cycle_start_with_set(head):
seen = set()
node = head
while node:
if node in seen:
return node
seen.add(node)
node = node.next
return NoneThis is the most direct answer and is correct. Its only drawback is the set, which can hold every node.
O(1) space: Floyd's two phases
Phase one: slow and fast start at the head; slow moves one step, fast two. If fast reaches null, there is no cycle. Otherwise they meet somewhere inside the loop. Phase two: put a new pointer at the head and move it and slow one step at a time. They meet at the entry.
def detect_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow is fast:
finder = head
while finder is not slow:
finder = finder.next
slow = slow.next
return finder
return Nonefunction detectCycle(head) {
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
let finder = head;
while (finder !== slow) {
finder = finder.next;
slow = slow.next;
}
return finder;
}
}
return null;
}Why phase two lands on the entry
Let a be the number of steps from the head to the entry, b the steps from the entry to where the pointers meet, and L the loop length. When they meet, slow has taken a + b steps and fast has taken twice that. Fast is ahead by a + b steps, and since both are on the loop, that lead must be a whole number of laps: a + b = kL for some k ≥ 1. Rearranged, a = kL - b.
Now read that in terms of walking. A pointer at the meeting point is b steps past the entry. Walking kL - b more steps brings it to the entry, because b + (kL - b) is exactly k laps. A pointer starting at the head reaches the entry after a steps. Since a = kL - b, both arrive on the same step. Both started counting from the head in phase one, which is what makes the arithmetic line up; starting fast one node ahead would shift it.
Time and space
| Approach | Time | Extra space |
|---|---|---|
| Set of visited nodes | O(n) | O(n) |
| Floyd's two phases | O(n): slow takes at most a + L steps in phase one, then a steps in phase two | O(1) |
| Mark nodes by changing them | O(n) | O(1), but breaks the no-modification rule |
Edge cases
- Empty list or one node without a loop: return null.
- A self-loop on the head: the pointers meet at the head, and phase two returns it at once.
- The loop covers the whole list, so the entry is the head.
- A long tail and a tiny loop, or a short tail and a long loop: both work, since k can be larger than 1.
- Repeated values: compare nodes, never values.
Mistakes to avoid
- Comparing slow.val == fast.val, which can stop at the wrong place when values repeat.
- Starting fast at head.next (as in some cycle detection code) and then running phase two unchanged; the offset breaks the a = kL - b relation.
- Moving the phase-two pointers at different speeds.
- Returning the meeting point as the entry; it is only somewhere inside the loop.
- Checking fast.next.next without first checking fast.next.
How to explain it out loud
Fictional example
- Candidate
- First, slow and fast from the head. If fast runs out, there is no loop. If they meet, they are inside the loop.
- Candidate
- Call the tail length a, the entry-to-meeting distance b, and the loop length L. Fast took twice as many steps as slow, so the difference, a plus b, is a whole number of laps.
- Candidate
- That means a equals k laps minus b. So from the meeting point, a more steps lands on the entry, and a steps from the head also lands on the entry. I move one pointer from each place until they meet. Linear time, constant space, no changes to the list.
Related walkthroughs
Happy Number applies the same fast and slow pointers to a number sequence instead of nodes. Intersection of Two Linked Lists uses another distance-balancing argument. Sort List uses slow and fast pointers to find a middle rather than a loop.
Where Natively fits
If you practice the proof on a Mac or Windows PC, Natively can read the problem from a screenshot and write an approach and code in the overlay, which you can compare with your own phase-two explanation afterwards. Transcribing the mock interview lets you check whether you defined a, b and L before using them. Read more on the AI interview assistant page.
Questions people ask
How do you find where a cycle starts in a linked list?
Move slow one step and fast two until they meet. Then move one pointer from the head and one from the meeting point, one step each; they meet at the start of the cycle.
Why does Floyd's algorithm find the cycle start?
If a is the distance to the entry, b from the entry to the meeting point and L the loop length, then a + b is a whole number of laps, so a equals that many laps minus b.
What is the time complexity of Linked List Cycle II?
O(n) time and O(1) extra space with Floyd's method. A hash set of visited nodes is also O(n) time but uses O(n) memory.
What does Linked List Cycle II return if there is no cycle?
Null. The fast pointer reaches the end of the list, which only happens when there is no loop.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
