Pattern #03
Fast & Slow Pointers
EssentialLinked-list cycles, middle nodes, and fixed pointer gaps.
When to use
Prefer on linked lists (or cyclic sequences) when you need the middle, detect a cycle, or maintain a gap of k nodes. without knowing the length up front.
Recognition cues
- Linked list cycle detection
- Find middle of list
- Remove nth node from end (gap of n)
- Palindrome linked list (find mid, reverse half)
Common pitfalls
- Null checks on fast.next before fast.next.next
- Off-by-one when positioning the gap for "nth from end"
- Forgetting to reconnect head when deleting the first node
90-second recognition drill
Which pattern fits best?
- Linked list cycle detection
- Find middle of list
- Remove nth node from end (gap of n)
Interactive
Mental model
A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.
How to think about it
Two pointers start on the same list and move at different speeds, or with a fixed gap. When the fast pointer finishes, the slow pointer sits on a useful node: the middle, the node before a deletion, or the meeting point inside a cycle. LC 160 is the exception on this path. The pointers switch lists instead of changing speed, so the two walks have the same length.
Template shapes
| Shape | Core move | Example |
|---|---|---|
| Cycle | Slow takes one step, fast takes two. They meet only inside a cycle. A null next on fast means there is no cycle |
LC 141 |
| Entrance | After they meet, one pointer restarts at the head. Both take one step and meet at the entrance | LC 142 |
| Middle | Both start at the head. When fast cannot take two more steps, slow is the middle. Even length returns the second middle | LC 876 |
| Gap of n | A dummy sits in front of the head. Fast walks n steps ahead, then both move until fast falls off. Delete slow.next |
LC 19 |
| Palindrome | Find the middle, reverse the second half, and compare. One node and an empty list are palindromes | LC 234 |
| Intersection | At null, each pointer continues from the other head. The walks meet at the intersection, or at null together | LC 160 |
Complexity baseline
O(n) time, O(1) extra space. That is why this beats storing the list.
From template to problem
- Both pointers start at the head, except LC 19, where a dummy sits in front so deleting the original head is the same splice.
- Read
fast.nextonly afterfastis non-null. The double step needs both checks. - The entrance walk is one step each. Keeping the two speeds after the first meeting does not land on the entrance (LC 142).
- LC 160 does not use two speeds. Switching heads equalizes the lengths. No intersection means both arrive at null on the same step.
Template
Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.
Show helper code
/** Fast/slow template: detect cycle (Floyd). */
export class ListNode {
val: number;
next: ListNode | null;
constructor(val = 0, next: ListNode | null = null) {
this.val = val; this.next = next;
}
}
/** Fast/slow template: detect cycle (Floyd). */
export class ListNode {
val: number;
next: ListNode | null;
constructor(val = 0, next: ListNode | null = null) {
this.val = val; this.next = next;
}
}
export function hasCycle(head: ListNode | null): boolean {
let slow = head, fast = head;
while (fast?.next) {
slow = slow!.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
export function hasCycle(head: ListNode | null): boolean {
let slow = head, fast = head;
while (fast?.next) {
slow = slow!.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
- 1#19 Remove Nth Node From End of ListGuidemedium
- 2#141 Linked List CycleGuideeasy
- 3#142 Linked List Cycle IIGuidemedium
- 4#160 Intersection of Two Linked ListsGuideeasy
- 5#234 Palindrome Linked ListGuideeasy
- 6#876 Middle of the Linked ListGuideeasy