Detect Cycle in Linked List
Floyd's Cycle Detection Algorithm with slow/fast pointers
Floyd's Cycle Detection (Python)
2 # Initialize both pointers at head● slow = head4 fast = head5 6 # Move until fast reaches end7 while fast and fast.next:8 slow = slow.next # Move 1 step9 fast = fast.next.next # Move 2 steps......12 if slow == fast:13 return True14 Current Line (3): Set slow pointer
Floyd's Cycle Detection
Setup
Step
0 / 8
Phase
Initialize
Slow Pointer
1
Algorithm Progress
Pointer State
Step Explanation
SetupInitialize 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