Detect a Loop in a Linked List
viaGlassdoor
Problem: Given a linked list, determine whether it contains a cycle (loop).
Constraints: List may or may not be circular; no modification of node values allowed.
Example: 1->2->3->4->2 (pointing back to node 2) contains a loop.
Approach: Floyd's Cycle Detection (tortoise and hare) - use two pointers, slow moving one step and fast moving two steps at a time. If they meet, a loop exists; if fast reaches null, there is no loop. O(n) time, O(1) space.
asked …