Pregunta de entrevista de Hughes Network Systems

How do you Catch Loops in Two Passes

Respuesta de la entrevista

Anónimo

5 ene 2014

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