Ch. 18 · Data Structures & Algorithms

Linked List Cycles and Fast-Slow Pointers

Linked List Cycles and Fast-Slow Pointers. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Two pointers advancing at different speeds can detect a cycle using constant auxiliary storage. Detection and locating entry are separate steps.

Before you start

You should know arrays, loops and basic complexity notation. Start with a small input you can trace by hand. State what each variable means and which invariant is maintained, then use that invariant to explain correctness before discussing performance or writing a more compact solution.

The practical goal is to reason through this situation: In a cycle, the faster pointer eventually meets the slower pointer. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Check termination safely

Fast and fast.next must exist before advancing twice.

Step 2: Detect a meeting

Different speeds eventually meet inside a reachable cycle.

Step 3: Locate the entry separately

Reset one pointer to head and advance both one step after a meeting.

Worked scenario

In a cycle, the faster pointer eventually meets the slower pointer.

A list has a noncyclic prefix leading into a loop. Detecting the loop does not identify its entry from the meeting position alone. The second phase uses the distance relationship established by the meeting; in an acyclic list fast reaches the end and that phase must not run.

Common mistake

Dereferencing fast.next without checking termination breaks acyclic input.

Verify the behavior

Test empty, one-node acyclic, self-loop and a cycle beginning after a prefix.

Interview exercise

Find the cycle entry.

Answer and reasoning

After a meeting, reset one pointer to the head and advance both one step until they meet at the entry.

Continue learning

Compare the scenario with the Data Structures and Algorithms interview questions and test your understanding with the Data Structures and Algorithms MCQs. For terminology and implementation details, consult the reference material.

More in Data Structures & Algorithms

read ✓Data Structures & Algorithms · mid

Linked List Reversal and Pointer Ownership

Linked List Reversal and Pointer Ownership. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
read ✓Data Structures & Algorithms · mid

Bit Manipulation Patterns for Interviews

Set, test and clear bits, exploit XOR properties, and count set bits with the Brian Kernighan trick.

~2 min readread →
read ✓Data Structures & Algorithms · hard

Backtracking and Safe Pruning Rules

Backtracking and Safe Pruning Rules. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
esc