Linked List Cycle

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Linked List Cycle

Approach

Use two pointers: slow moves one step at a time, fast moves two steps. If a cycle exists, fast will eventually lap slow and they will meet. If fast reaches null, there is no cycle.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Linked List (Fast/Slow Runner)

Why It Works

In a cycle, the fast pointer closes the gap by one node per step. Since the gap shrinks monotonically, the two pointers are guaranteed to meet within one full traversal of the cycle.

Updated Feb 2026