Middle of the Linked List
Tortoise and Hare algorithm with slow/fast pointers
Tortoise & Hare (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 steps10 11 # slow is now at middle12 return slowCurrent Line (3): Set slow pointer
Tortoise & Hare
INIT
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