Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

A singly linked list has nodes at positions 0..4 holding 19 -> 83 -> 57 -> 22 -> 13, but the last node's next pointer points back to the node at position 1 (a cycle), instead of None. Using Floyd's cycle detection (slow moves 1 step, fast moves 2 steps), what happens?

  1. A. The program crashes immediately with a null pointer dereference on the first step
  2. B. Slow and fast pointers eventually meet inside the cycle, proving a cycle exists
  3. C. Fast pointer reaches None first, proving there is no cycle
  4. D. Slow and fast pointers never meet because fast always stays exactly one node ahead

Correct answer: B. Slow and fast pointers eventually meet inside the cycle, proving a cycle exists

Since the list has a cycle, the fast pointer can never reach None; because fast closes the gap to slow by one node per step inside the cycle, they are guaranteed to meet.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic