Quant Memo
Coding/●●●●●

Detect a cycle in a linked list

A misbehaving feed handler built a linked list of messages, and a bug may have spliced a node's next pointer back to an earlier node, so traversal never terminates.

1 -> 2 -> 3 -> 4
          ^    |
          +----+        # 4 points back to 3: cycle

Detect whether the list contains a cycle in O(n)O(n) time and O(1)O(1) space. A hash set of visited nodes works but costs O(n)O(n) memory, the interviewer wants Floyd's algorithm.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions