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

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

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

Correct answer: A. 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