Pattern · Interview prep
Fast and Slow Pointers
Walk a chain with two runners at different speeds. The gap between them measures the middle, a loop, or a node counted from the end, without an index and without a second full pass.
What it is
What is the fast and slow pointers pattern?
A singly linked list has no indexes. You can only follow next. Two pointers still work if they move at different speeds, or if one of them keeps a fixed lead. When the faster runner cannot continue, the slower runner is sitting on the node you wanted.
- Double speed. Slow steps once. Fast steps twice. When fast cannot take another pair of steps, slow is halfway. If fast lands on the same node as slow, the chain loops.
- A fixed gap. Fast walks n steps first. Then both move one step at a time. When fast reaches the end, slow is n nodes behind it.
Fast is on the last node, so slow is on the middle
When to use it
How do you recognize a fast and slow pointers problem?
Read the statement for these signals before you copy the chain into an array.
Read the full recognition guide: Clues that a problem needs two pointers
Phrases to watch for
- middle of the list
- cycle
- nth from the end
- singly linked
- constant extra memory
- one pass
- next pointer
- 1
You need a position you cannot index
The middle, or a node counted from the tail, is a distance. A second runner measures it while the first one walks.
- 2
A repeat would be a node, not a value
A cycle means some next pointer leads back to an earlier node. Two speeds meet inside that loop and never meet on a list that ends.
- 3
Extra memory is the constraint
A set of seen nodes, or an array of values, would answer the question and then fail the space limit.
Looks similar, but isn’t
Both ends of a sorted row
A pair on a sorted array moves one index inward after each comparison. That is Two Pointers. A chain has no left and right index.
A contiguous slice with a running property
Sliding Window adds one end and drops the other on an array or a string. The two speeds here are runners on next pointers.
You may use a second pass and linear memory
Counting the length, then walking halfway, finds a middle. The pattern is the single walk that keeps the answer on the slow runner.
Worked example
How does double speed find the middle?
Start both runners on the head. Slow moves one node. Fast moves two. Stop when fast cannot take another two-step move. On 1 → 2 → 3 → 4 → 5, slow is then on 3. On an even length, the same stop lands on the second of the two middle nodes. This is the chain you can practice below.
type Node = { value: number; next: Node | null }; function middleValue(head: Node | null): number | null { let slow: Node | null = head; let fast: Node | null = head; while (fast !== null && fast.next !== null) { slow = slow === null ? null : slow.next; fast = fast.next.next; } return slow === null ? null : slow.value;}One step for slow, two for fast, and only while fast can take both.
Code is shown in TypeScript. You can solve problems in TypeScript, JavaScript, or Python.
Complexity
- TimeO(n)
Fast reaches the end after a linear number of steps. Slow has moved about half as far.
- SpaceO(1)
Only the two node references are stored. The chain is not copied.
An even length
On 1 → 2 → 3 → 4, the next loop moves slow to 3 and fast off the end. The stop is the second middle, 3, not 2.
A cycle instead of a middle
Use the same two speeds, and compare the nodes rather than the values. If fast lands on slow, some next pointer leads backward. If fast reaches null, the chain ends.
Step by step on 1 → 2 → 3 → 4 → 5
- 1
Both runners start on 1. Fast can still move two nodes, so the loop runs.
1L·R2345 - 2
Slow moves to 2. Fast moves to 3. Fast still has a next node, so both move again.
12L3R45 - 3
Slow moves to 3. Fast moves to 5. Fast has no next node, so the loop stops. The middle value is 3.
12345
Common mistakes
What mistakes do people make with fast and slow pointers?
Stepping fast before checking its next
fast.next.next throws when fast is on the last node. The loop condition has to see both the node and the one after it.
InsteadContinue only while fast and fast.next both exist. Then one step and two steps are safe.
Stopping one step early on an even list
If you halt when fast is on the last node of an even chain, slow is still on the first of the two middle nodes.
InsteadHalt only when fast cannot take two steps. That extra loop is what moves slow onto the second middle.
Treating equal values as a cycle
Two nodes can store the same number and still be a straight list. A cycle is the same node reached again.
InsteadCompare the node references. A matching value is not a loop.
Losing the head when the gap reaches the first node
The nth node from the end can be the head. Unlinking it without a node in front of it leaves you holding the old head.
InsteadPut a dummy node before the head. The slow runner stops on the dummy when the node to drop is the first one, and the returned head is dummy.next.
Practice it in Algofily
Which Algofily problems train fast and slow pointers?
4 problems are tagged Fast and Slow Pointers. They run from the middle of a chain and a cycle to a gap from the end and a weave of the two halves.
- Locked: opens after you create an account. Middle of the ChaineasyFast and Slow PointersYou receive the head of a singly linked list.Algofily 40Open problem
- Locked: opens after you create an account. Loop in the ChaineasyFast and Slow PointersDecide whether a singly linked list contains a cycle.Algofily 40Open problem
- Locked: opens after you create an account. Drop the Nth From the EndmediumFast and Slow PointersRemove the nth node from the end of a singly linked list.Algofily 40Open problem
- Locked: opens after you create an account. Weave the ChainmediumFast and Slow Pointers · Linked ListReorder a singly linked list so the nodes alternate from the front and the back.Algofily 40Open problem
A lock means the problem opens after you create an account. Pair With Target is open to try without one.
Start Learning.
Work Pair With Target through all seven stages. An account saves that progress and opens every other problem.