SHARE
TWEET

Untitled

a guest Dec 22nd, 2013 47 Never
  1. First, eliminate the pathological case of a 3-element list with a cycle from element 2 to 1. Keep two
  2. pointers. One at element 1 and the next at element 3. See if they are equal; if not, move P1 by one and
  3. P2 by two. Check and continue. If P1 or P2 is null, there is no loop. If there is one, it will definitely be
  4. detected. One pointer will eventually catch up with the other (i.e., have the same value), though it
  5. might take several traversals of the cycle to do it.
RAW Paste Data
Top