Lesson 19 of 86BeginnerPro Interactive Lesson

Fast and Slow Pointers: Midpoint and Cycles

Compare one-step and two-step advances. First use them to find a midpoint, then check for a cycle.

Core Intuition & How It Works

Floyd’s tortoise-and-hare technique advances a slow pointer by one node and a fast pointer by two. In an acyclic list, slow reaches the midpoint when fast hits null; in a cyclic list, the two pointers are guaranteed to meet inside the cycle.

In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.

Time & Space Complexity

MetricComplexity
Time ComplexityO(n)
Space ComplexityO(1)
Topic ChapterChapter 4: Linked Lists
Difficulty LevelBeginner

Key Pattern to Recognize

Check fast !== null && fast.next !== null before advancing fast by two steps; reset one pointer to head after a meeting to find the cycle entry.

Common Pitfalls & Edge Cases

Checking only fast !== null and crashing on fast.next.next, or off-by-one midpoint choice on even-length lists.

Real-World & Technical Interview Applications

Cycle detection in linked lists, finding list midpoints for merge sort, and detecting duplicate numbers in O(1) space.

Unlock the full course

All 86 lessons across 15 chapters

View pricing

Delete account

This permanently deletes your account and ends any Pro access.