Pergunta de entrevista da empresa Amazon

Find if a linked list has a cycle in it.

Respostas da entrevista

Sigiloso

24 de mar. de 2010

You can define two pointers, slow and fast. Make both of them start from the head. Advance the fast two items at a time and the slow one item at a time. If the link list is acyclic, the fast pointer will reach a NULL. If the link-list is cyclic, you will get to a point where either "fast = slow" or "fast->next = slow". This algorithm is O(n).

7

Sigiloso

14 de fev. de 2011

The ISU author's answer is fantastic.