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?
- A. Slow and fast pointers eventually meet inside the cycle, proving a cycle exists
- B. Slow and fast pointers never meet because fast always stays exactly one node ahead
- C. The program crashes immediately with a null pointer dereference on the first step
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Selection sort (ascending, swap the minimum of the unsorted suffix into place each pass, skipping the swap if it is already in place) is…
- How many adjacent swaps does bubble sort (ascending) make on [97, 9, 32, 5, 21, 45, 53, 34]?
- Standard top-down merge sort is run on [3, 48, 14, 99, 61, 71, 8, 15]. How many element comparisons happen during all the merge steps…
- Quicksort with Lomuto partitioning (last element as pivot) sorts [84, 38, 96, 7, 19, 64, 7, 94]. How many swaps happen in total, across…
- 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…
- A singly linked list holds 50 -> 31 -> 76 -> 53 -> 96 -> None (in that order, 5 nodes). Using the standard two-pointer technique (advance…