Browse curriculum

Linked List Cycle II

Return the node where a linked list's cycle begins, or null, using Floyd's cycle detection and a second walk from the head, in O(1) extra space.

Problem Understanding

Linked List Cycle II: given the head of a linked list, return the node where the cycle begins, or null if there is no cycle. LeetCode describes the input with pos, the index the tail's next points to (-1 for none); pos is not passed to your function. Do not modify the list.

Examples: [3,2,0,-4], pos 1 → the node at index 1; [1,2], pos 0 → index 0; [1], pos -1 → null.

The Intuition: Two Walks of the Same Length

Phase 1 is Linked List Cycle: slow moves one node, fast moves two. If fast reaches the end there is no cycle; otherwise they meet somewhere inside the cycle.

Phase 2 uses some arithmetic. Say the cycle starts a nodes from head and they met b nodes into a cycle of length c:

  1. slow walked a + b; fast walked 2(a + b).
  2. The extra a + b that fast walked is a whole number of laps, so a + b is a multiple of c.
  3. From the meeting point, a more nodes completes those laps and ends at the cycle start.
  4. So send slow back to head and move both one node at a time: after a steps both are at the start.

The same slow and fast pointers solve Middle of the Linked List and Find the Duplicate Number, which is this exact problem in disguise.

Interactive Walkthrough

POS = 1 · TAIL → #1

START AT HEAD

head slow fast

3

#0

→

2

#1

→

0

#2

→

-4

#3

↺ back to #1

Phase

1

Moves

0

Answer

:

 

slow and fast both start at head, #0 (3)

Next

fast is #0 (3) and fast.next is #1 (2): both exist, keep running

Values

pos

1

Legend & Complexity

slow: one node per step; back at head in phase 2

fast: two nodes per step, then one in phase 2

The node where the cycle begins

↺ back to …: the tail’s next points back into the list

Time

O(n)

Space

O(1)

slow and fast are named above their nodes, and the row ends ↺ back to #k where the tail links back. Watch where they meet, then the second walk. Try Long tail to see the meeting point and the start come apart.

Dry Run Table

Input: [3,2,0,-4], tail → #1 (a = 1, c = 3).

Step slow fast Note
start #0 #0
move 1 #1 #2
move 2 #2 #1 fast went #3 → #1
move 3 #3 #3 meet: a cycle exists
reset #0 #3 slow back to head
walk 1 #1 #1 meet again

Answer: the node at index 1 (value 2). Here a + b = 1 + 2 = 3, one full lap.

The Solution Template

Phase 1 detects, phase 2 runs inside the if.

Linked List Cycle II Code
1var detectCycle = function(head) {
2 let slow = head, fast = head;
3 while (fast && fast.next) {
4 slow = slow.next;
5 fast = fast.next.next;
6 if (slow === fast) {
7 slow = head;
8 while (slow !== fast) {
9 slow = slow.next;
10 fast = fast.next;
11 }
12 return slow;
13 }
14 }
15 return null;
16};

Edge Cases & Common Mistakes

  • Returning the meeting point: it is inside the cycle but usually not its start.
  • Moving fast two steps in phase 2: both pointers must move one node at a time.
  • Cycle starting at head: they meet and the walk from head is already there; the inner loop runs 0 times.
  • Empty list or one node without a loop: the while check fails at once; return null.
  • Comparing values instead of nodes: values can repeat; compare the node references.

Summary

Run slow and fast until they meet (or fast ends: no cycle). Then reset slow to head and move both one node at a time; they meet at the cycle start. O(n) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Detect

    Move slow one node and fast two until they meet, or fast reaches the end (no cycle: return null).

  2. Reset

    Put slow back at head and leave fast at the meeting point.

  3. Walk

    Move both one node at a time; the node where they meet is the start of the cycle.

Frequently asked questions

Why does resetting one pointer to head find the cycle start?

Let the cycle start a nodes from head, let slow and fast meet b nodes into the cycle, and let the cycle have length c. Slow walked a + b nodes and fast walked twice that, and the extra a + b nodes fast walked are whole laps, so a + b is a multiple of c. From the meeting point, walking a more nodes therefore lands exactly on the cycle start, which is also where a walk of a nodes from head arrives. So the two pointers meet there.


Is the meeting point the start of the cycle?

Not in general. In LeetCode example 1, [3, 2, 0, -4] with the tail back to index 1, slow and fast meet at index 3, while the cycle starts at index 1. Only the second walk finds the start.


Why not use a hash set?

Storing every visited node and returning the first one seen twice works and is O(n) time, but it uses O(n) memory. The follow-up asks for O(1) memory, which the two-pointer method gives.


How is this different from Linked List Cycle?

Linked List Cycle (LeetCode 141) only asks whether a cycle exists, so it returns as soon as slow and fast meet. Linked List Cycle II continues from that meeting point with a second phase to find the node where the cycle begins.

New to the technique behind this problem? Watch it run step by step:

Singly Linked List Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description