Skip to content
DSA Patterns
Menu
Language

Pattern #03

Fast & Slow Pointers

Essential

Linked-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.

Step 1 of 8
1/8

Slow steps +1, fast steps +2. No need to know the list length.

slowfast
1
2
3
4
5
Stateslow=1fast=1
Key:CurrentActive rangeLeft / slowRight / fastMatchSettled

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

  1. Both pointers start at the head, except LC 19, where a dummy sits in front so deleting the original head is the same splice.
  2. Read fast.next only after fast is non-null. The double step needs both checks.
  3. The entrance walk is one step each. Keeping the two speeds after the first meeting does not land on the entrance (LC 142).
  4. 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.

Fast & Slow Pointers · Template
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;
}