- Used for finding cycles in linked list in
- Uses two pointers, and , pointing at head initially
- moves one step, moves two steps at a time
- Check if at any point both pointers point to same node
- Reset back to head of linked list
- Move both pointers one step at a time
- The point they meet at is starting point of cycle
# links
links = [1, 2, 3, 4, 5, 6, 2]
# list head
head = 0
slow = links[head]
fast = links[links[head]]
while slow != fast:
# move slow once, fast twice
slow = links[slow]
fast = links[links[fast]]
# reset slow to head
slow = head
while slow != fast:
# move both once
slow = links[slow]
fast = links[fast]
print(slow)