PROBLEM 21 OF 75

Linked List Cycle

Return whether a linked list contains a cycle.

PATTERNLinked-list rewiringSave next before you change next.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Return whether a linked list contains a cycle.

WHY THIS FITS

A fast pointer eventually laps a slow pointer only inside a cycle.

COMPLEXITY

O(n) time · O(1) space

COMMON FAILURE

Comparing node values instead of node identity produces false positives.

THE REASONING, IN ONE PLACE

How to solve Linked List Cycle

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Return whether a linked list contains a cycle.

Why Linked-list rewiring fits

A fast pointer eventually laps a slow pointer only inside a cycle.

State to maintain

Slow moves one edge; fast moves two.

Transition

Advance while fast and fast.next exist; equal pointers prove a cycle.

Time and space

O(n) time · O(1) space

Counterexample to the tempting mistake

Comparing node values instead of node identity produces false positives.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗