GATE Computer ScienceHardGATE CS (PYQ pattern)

A routine rev(k) reverses every block of k consecutive nodes of a singly linked list in place, leaving a final block of fewer than k nodes untouched. The list 1 -> 2 -> ... -> 11 is processed by rev(3) and then by rev(4). Which nodes are now at positions 4 and 10 (1-indexed)?

  1. A. 7 and 10
  2. B. 3 and 10
  3. C. 3 and 11
  4. D. 6 and 10

Correct answer: B. 3 and 10

rev(3) gives 3 2 1 6 5 4 9 8 7 10 11 (the tail 10 11 is shorter than 3, so it stays). rev(4) then reverses positions 1-4 and 5-8 and leaves the 3-node tail: 6 1 2 3 8 9 4 5 7 10 11. Positions 4 and 10 hold 3 and 10. Doing rev(4) first gives a different list, and reversing the short tail too changes position 10.

More Programming and Data Structures (GATE) questions

Exams that ask this topic