Floyd's Cycle Detection
Floyd's Tortoise and Hare algorithm detects a cycle using two pointers moving at different speeds. The slow pointer moves one node at a time, while the fast pointer moves two. If a cycle exists, the two pointers will eventually meet inside the cycle.
Time complexity: O(n).
Auxiliary space: O(1).
The algorithm does not require a HashSet of visited nodes.
The meeting point confirms a cycle but is not necessarily the cycle's entry point.
The same technique can be extended to locate the cycle entry.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience