Detect if a linked list has a cycle using two pointers (tortoise and hare). Β β± Time
O(n)Β πΎ SpaceO(1)
Use two pointers: slow moves 1 step at a time, fast moves 2 steps. If there's a cycle, the fast pointer will eventually lap the slow pointer and they'll meet. If there's no cycle, fast reaches None first.
Algorithm:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast: return True β they met β cycle!
return False
No cycle: A β B β C β D β None
fast hits None β return False
With cycle: A β B β C
β β
D β E
Step 1: slow=B, fast=C
Step 2: slow=C, fast=E
Step 3: slow=D, fast=D β MEET! β cycle detected β
Known as Floyd's cycle detection algorithm, published by Robert W. Floyd in 1967. Also called the "tortoise and hare" algorithm. It's used in cryptography (Pollard's rho algorithm for integer factorisation) and random number generator cycle detection.
- O(1) space is the key advantage over the hash set approach (which is O(n))
- The fast pointer must check
fastANDfast.nextbefore advancing - Finding the cycle entry point: after detection, reset one pointer to head, advance both 1 step at a time β they meet at the entry node
- Works for any iterable sequence, not just linked lists
- Follow-up: what's the cycle length? After meeting, keep one fixed and count steps until they meet again
- Linked list cycle detection (LeetCode 141, 142)
- Detecting loops in iterators / generators
- Cryptographic applications (Pollard's rho)
- Memory leak detection in reference-counted systems
- Detecting infinite loops in state machines
- Detect Cycle β cycle detection in general graphs
- Reverse Linked List β another classic linked list problem
- Rotate Linked List β linked list manipulation
- Merge K Lists β linked list merging