Linked List Cycle

easy

Detect if a linked list has a cycle using constant space

Linked List Cycle

Key Insight

Fast moves 2x; if there's a cycle, fast laps slow — they MUST meet

Step 1Setup
Cycle: 5 → 3
slow
fast
1
2
3
4
5
↩ cycle

List has a cycle: 5→3. Slow and fast both start at head.

1 / 5
Practice the Code

Step-by-Step Walkthrough: Linked List Cycle

Fast moves 2x; if there's a cycle, fast laps slow — they MUST meet

  1. Setup

    List has a cycle: 5→3. Slow and fast both start at head.

  2. Step 1

    Slow moves to 2. Fast moves to 3 (two steps).

  3. Step 2

    Slow moves to 3. Fast moves to 5 (two steps: 3→4→5).

  4. They Meet!

    Slow moves to 4. Fast moves to 4 (two steps: 5→3→4). They collide at node 4.

  5. Cycle Detected

    Slow and fast met, confirming a cycle exists. Return true.