Fast & Slow Pointers Pattern
Floyd's tortoise-and-hare moves one pointer twice as fast as the other. Where they meet reveals cycles, midpoints, and cycle entry points — all in O(1) extra space.
Typical complexity: O(n) time, O(1) space
When to use it
- You suspect a cycle in a linked list or number sequence.
- You need the middle of a list in one pass.
- You want O(1) space where a hash set would be O(n).
Practice problems
Read the approach or generate an interactive, step-by-step lesson:
Master fast & slow pointers by doing
codebrew.ai turns each problem into an interactive lesson — trace the algorithm variable by variable until the pattern clicks.
Start a free lesson