Amazon Interview Question

Find if a linked list has a cycle in it.

Interview Answers

Anonymous

Mar 24, 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

Anonymous

Feb 14, 2011

The ISU author's answer is fantastic.