Fast & Slow Pointers
O(n) / O(1)
Move one pointer 1 step and another 2 steps; they meet inside a cycle, and slow lands on the middle when fast hits the end.
0/11 solved
Recognise it
The signal
Linked-list cycle or middle in one pass with O(1) space, or a chain x → f(x) that must repeat (Happy Number, Find the Duplicate).
1Brute force
Store visited nodes in a hash set.
2The observation
O(n) memory: the speed difference guarantees meeting inside a cycle.
3Why it works
In a cycle the fast–slow gap shrinks by 1 per step, so they meet; then restart one at head, step both by 1, they meet at the entry.
Reach for it when
- +A linked list that might have a cycle, or you need its middle in one pass.
- +O(1) space required (can't use a visited set / hash).
- +A sequence defined by repeatedly applying a function (
Happy Number,Find the Duplicate) — a hidden cycle.
Not when
- ×A hash set is affordable and you want simpler codeuse Visited set
- ×You have random access (array) and want the middleuse Index
len // 2 - ×Nodes have several successors, or you need the cycle members of a general graphuse DFS with colouring
Intuition
Two pointers walking the same list at different speeds — slow moves 1 node per step, fast moves 2. It's the trick behind cycle detection and finding the middle in one pass, and it needs O(1) extra space (no visited set).
Read the full intuition
Finding the middle: when fast reaches the end, it has gone twice as far, so slow is exactly halfway. One pass, no length count.
Detecting a cycle (Floyd's tortoise & hare): picture two runners on a circular track — the faster must eventually lap and meet the slower. If fast ever equals slow, there's a loop. If fast hits null, the list ends → no loop. The faster runner closes the gap by exactly 1 node each step, so they're guaranteed to collide inside any cycle.
Finding the cycle's start (a beautiful bonus): after they meet, reset one pointer to the head and move both one step at a time; they meet again exactly at the cycle's entrance. (It falls out of the distance algebra — worth memorizing, not re-deriving in an interview.)
Key idea
The same idea generalizes beyond linked lists: "Happy Number" and "Find the Duplicate Number" become cycle problems where
nextis "apply this function" — spotting that disguise is the real skill.
Why it costs what it costs
Time O(n). fast traverses the list at most twice before meeting slow or hitting the end.
Space O(1). two pointers, no auxiliary structure — the whole point versus a visited set.
Practice
- Middle of the Linked ListFoundationFoundationAmazon, AdobeEasy
- Linked List CycleApply it · Detect a cycle.Apply itAmazon, MicrosoftEasy
- Happy NumberOne change · The 'list' is an implicit sequence x -> f(x).One changeAdobe, AppleEasy
- Linked List Cycle IITwists · Find the entry, not just existence.TwistsAmazon, AdobeMedium
- Palindrome Linked ListCombine · Find the middle, reverse the second half, compare.CombineAmazon, AdobeEasy
- Reorder ListCombine · Middle + reverse + interleave merge.CombineAmazon, MetaMedium
- Find the Duplicate NumberMastery checkMastery checkMedium
- Remove Nth Node From End of ListAmazon, MetaMedium
- Sort ListAmazon, AdobeMedium
- Intersection of Two Linked ListsAmazon, MicrosoftEasy
- Throw the Ball (k up to 1e12)OracleOAMedium
Pitfalls
Classic bugs
- !Guard is
while fast and fast.next— checking onlyfastcrashes onfast.next.next. - !
slow=fast=headgives the second middle of an even list; startfast=head.nextfor the first. - !Compare node identity (
is/===), not values — different nodes can hold equal values. - !The meeting point is not the cycle start: run phase two, moving both one step (not fast at 2×).
- !In Happy Number / Find the Duplicate,
nextisf(x)ornums[x], not a.nextpointer — model it that way.
Go deepervariants and techniques · 1 combinations · resources
Variants and techniques
- Cycle detectionMeet ⇒ cycle, fast hits null ⇒ none. Linked List Cycle.
- Find middleslow lands on the midpoint when fast ends. Middle of the Linked List.
- Cycle entranceAfter meeting, reset one to head, advance both by 1, meet at the start. Linked List Cycle II.
- Function iterationTreat x→f(x) as 'next'; a repeat means a cycle. Happy Number, Find the Duplicate Number.
- Palindrome listFind middle, reverse the second half, compare. Palindrome Linked List.
Combinations
Fast & Slow Pointers+In-place Reversal
palindrome list, reorder list
- Fast & Slow Pointers:
- finds the middle
- In-place Reversal:
- reverses the second half