Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- First, eliminate the pathological case of a 3-element list with a cycle from element 2 to 1. 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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement