Floyd’s Cycle Detection Algorithm is a pointer algorithm that makes use of only two pointers, which move through the given sequence at different speeds interval. The idea of algorithm is to move fast pointer twice as quickly as the slow pointer and the distance between the two increases by 1 at each step. If at some point both the points meet, we have a cycle in the list, else if we have reached the end of the list, no cycle is present. This algorithm is also called the “tortoise and the hare algorithm”.
Let's see a simple implementation of Floyd Cycle Detection Algorithm in Java.
Goodness, gracious it has been a long time since I have seen a Floyd circle detector. The algorithm is interesting purely due to its naivety. Even a deterministic Carson long chain discriminator will work more effectively than the Floyd detector.