IRInterview Ready
← All patterns

Fast & Slow Pointers (Floyd's)

Two pointers moving at different speeds through a linked list or implicit sequence to detect cycles or find midpoints.

When to use it

  • Cycle detection in a linked list or a functional graph (each node has exactly one outgoing edge).
  • Finding the middle of a linked list in one pass.
  • Finding the start of a cycle (Floyd's algorithm second phase).

Signals in the problem statement

  • Linked list problems
  • 'Detect a cycle' or 'happy number'-style implicit-graph problems.

Common pitfalls

  • Null-checking `fast` and `fast.next` before advancing to avoid a null pointer exception.

Practice Problems

Linked List Cycle Detection

Easy

Asked at: Amazon, Microsoft

Given the head of a linked list, determine if it contains a cycle.

Time: O(n) · Space: O(1)

Reverse a Linked List

Easy

Asked at: Amazon, Microsoft, Apple

Reverse a singly linked list, in place, and return the new head.

Time: O(n) · Space: O(1)