C Programming - Find a Linked List is cyclic or not (Vintage Method)

Two pointer method:

Keep two pointers. One at element 1 and the next at element 3.
See if they are equal; if not, move P1 by one and P2 by two. Check and continue. If P1 or P2 is null, there is no loop. If there is one, it will definitely be detected. One pointer will eventually catch up with the other (i.e., have the same value), though it might take several traversals of the cycle to do it.


About this entry