Detect Cycle in Linked List

Floyd's Cycle Detection Algorithm with slow/fast pointers

Read Here
Step0 / 8
PhaseInitialize
Nodes5
Cycle
Back To Linked List

Floyd's Cycle Detection (Python)

2 # Initialize both pointers at head
slow = head
4 fast = head
5
6 # Move until fast reaches end
7 while fast and fast.next:
8 slow = slow.next # Move 1 step
9 fast = fast.next.next # Move 2 steps
......
12 if slow == fast:
13 return True
14
Current Line (3): Set slow pointer

Floyd's Cycle Detection

Setup

Step

0 / 8

Phase

Initialize

Slow Pointer

1

null1next2next3next4next5nextslowfast

Algorithm Progress

Cycle Status

Phase

Initialize

Pointer Positions

Slow (Tortoise)

1

Fast (Hare)

1

Algorithm Insight

Floyd's algorithm uses two pointers moving at different speeds. If they meet, a cycle exists. If fast reaches the end, there's no cycle. O(n) time, O(1) space.

Press "Start" to initialize slow and fast pointers at head.

Pointer State

slow1
fast1

Why This Step Matters

Both pointers start at head. Slow moves 1 step, fast moves 2 steps per iteration.

Current Action

Initialize: slow = head, fast = head

Step Explanation

Setup

Initialize Pointers

Set both slow and fast pointers to the head of the list.

  • > Phase: Setup
  • > Both pointers start at the same position
  • > This is the starting point of the algorithm
  • > Both pointers start at the same position.
Unvisited
Current (Slow)
Done
Fast Pointer