Fast / Slow Pointers
What is this?
A linked list is a chain of nodes where each one only points to the next โ like a treasure hunt of clues you can only follow forward. Since you can't jump to a position, you send two walkers down the chain at once: one fast and one slow. By watching where the slow walker is when the fast one reaches the end, you learn things like the middle of the list or where two chains join, all in a single pass.
๐ก Fun fact: The fast-and-slow trick is also called Floyd's tortoise and hare algorithm, named after Robert W. Floyd; if the two walkers ever land on the same node, the list secretly loops back on itself.
๐ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: You can't index a linked list, so you bring the answer to you with a second pointer. Move one pointer faster than the other (or keep a fixed gap behind it), and a single forward pass reveals the midpoint, the nth-from-end, or where two lists join โ all in O(1) memory.
Why a second pointer is the whole trick
A forward-only list hides anything that depends on position relative to the end or total length โ you'd normally need a first pass to measure. A cleverly-moving second pointer encodes that information while you walk:
- Different speeds (fast = 2ร slow): when fast reaches the end, slow is at the middle (and if there's a cycle, they meet).
- Fixed gap (fast starts
nahead): when fast falls off, slow sits exactly where the nth-from-end action happens. - List-switching (walk AโB and BโA): both pointers traverse
lenA + lenB, cancelling the length difference so they align.
The three problems
- Middle of the Linked List โ slow ร1, fast ร2; fast at end โ slow at middle (return the second middle on even length). One pass.
- Intersection of Two Linked Lists โ pointer a does A-then-B, b does B-then-A; after
lenA+lenBsteps they meet at the join (or both reachNoneif disjoint). No length math. - Remove Nth Node From End โ advance fast
nnodes, then move both until fast hits the end; slow is just before the target. A dummy head makes removing the head uniform.
The pattern
| Variant | Setup | Lands you at |
|---|---|---|
| Different speed | fast = 2 ร slow | the midpoint (or cycle meet-point) |
| Fixed gap | fast starts n ahead |
the nth-from-end |
| Switch lists | a: AโB, b: BโA | the intersection / None together |
The insight: relative motion is position. Half-speed โ halfway; a gap of
nโnfrom the end; equal total distance โ aligned. Pick the motion that encodes what you need, and one pass does it in O(1) space.
๐ Draw it yourself
- Tortoise & hare. Draw a 6-node list; step slow (ร1) and fast (ร2) and mark where slow lands when fast exits โ the midpoint.
- The gap. For Remove Nth, draw two pointers
napart and slide them together until the front leaves the list; the back is one before the target.
Snap photos and embed them with the /host-diagrams skill.
Key takeaways
- A second pointer replaces a length pass โ different speed, fixed gap, or list-switch.
- Tortoise & hare finds the midpoint (and cycles) in one pass, O(1) space.
- Fixed gap of
nfinds the nth-from-end; pair it with a dummy head. - Switch-lists cancels the length gap between two lists โ no counting.
- Why interviewers love it: it shows you can extract position/length info from a forward-only structure without extra memory.
Order: Middle of the Linked List โ Intersection of Two Linked Lists โ Remove Nth Node From End.
Remove Nth Node From End of List
Core idea: Open up a fixed gap of
nnodes between two pointers, then slide them together โ when the front pointer falls off the end, the back pointer is parked exactly one step before the node you must delete.
You have a singly linked list. You can only walk forward, one next at a time. Someone asks you to delete the node that sits n steps from the back. The trouble is obvious: you can't count from the back. There is no prev, no random access, no length handed to you. This lesson is about turning that "count from the end" request into something a forward-only walker can actually do โ ideally in a single pass.
Problem, rephrased
Imagine a browser history stored as a singly linked list, newest page first, oldest page last. You walk it front-to-back only. A privacy feature says: "forget the n-th oldest page I visited." That is the n-th node from the end. Remove it, splice the list back together, and return the (possibly new) head.
Rules of the game
- The list is singly linked: each node knows only its successor.
nis a valid 1-based index counted from the end:n = 1means the last node,n = lengthmeans the head itself.- Return the head of the modified list.
| Input list | n | Output list | What got removed |
|---|---|---|---|
1 โ 2 โ 3 โ 4 โ 5 |
2 | 1 โ 2 โ 3 โ 5 |
4 (2nd from end) |
1 โ 2 โ 3 |
3 | 2 โ 3 |
1 (the head) |
1 โ 2 โ 3 |
1 | 1 โ 2 |
3 (the tail) |
7 |
1 | `` (empty) | the only node |
That second row โ n == length, deleting the head โ is the case that quietly breaks naive code. Keep an eye on it.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson