Fast & Slow Pointers
Two pointers at different speeds to find cycles and midpoints.
- result
Both start at 1
slow will take one step at a time, fast two.
VARIABLES
- slow
- 1
- fast
- 1
Intuition & complexity
The core mental model, trade-offs, and Big-O cost.
What it is
A slow pointer moves one step while a fast pointer moves two. If there’s a cycle, fast eventually laps slow; if not, fast reaches the end.
Why it matters
It detects cycles and finds the middle in O(n) time with O(1) memory - no visited set needed.
Think of it like this
Two runners on a track: if it’s a loop, the faster one will lap the slower one.
Use it when
- Detecting a cycle in a linked list
- Finding the middle node
- Finding where a cycle starts (Floyd’s algorithm)
Avoid it when
- A visited set is affordable and you want simpler code
- Arrays where indices make the answer trivial
Complexity cheatsheet
| Operation | Time | Space |
|---|---|---|
| Cycle detection | O(n) | O(1) |
| Visited-set alternative | O(n) | O(n) |
| Find the middle | O(n) | O(1) |
Often confused with: two pointers - usually compares values; fast and slow only compares positions.
How to recognize it
Clues in interview prompts that point straight to this pattern.
- “O(1) extra space” on a linked-list cycle question means Floyd’s algorithm
- Values in 1..n can be treated as next pointers
Mistakes & edge cases
Catch common off-by-one bugs and corner cases before submitting.
Common mistakes
- Reading fast.next.next without checking fast and fast.next first
- Comparing values instead of node identity (use is)
- Starting fast one step ahead and getting the middle wrong for even lengths
Edge cases to test
- An empty list or a single node
- A node pointing to itself
- Even vs odd length when finding the middle
Knowledge check
Quick check to lock in the intuition.
Why must fast eventually meet slow inside a cycle?
Practice
Apply what you just watched on real interview problems.