Engaged Employer
How do you Catch Loops in Two Passes
Anonymous
Simultaneously go through the list by ones (slow iterator) and by twos (fast iterator). If there is a loop the fast iterator will go around that loop twice as fast as the slow iterator. The fast iterator will lap the slow iterator within a single pass through the cycle. Detecting a loop is then just detecting that the slow iterator has been lapped by the fast iterator. Ref:http://ostermiller.org/find_loop_singly_linked_list.html
Check out your Company Bowl for anonymous work chats.