Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT

A linked list holds the values [1, 2, 3, 4, 5, 6, 7, 8]. Reverse every complete group of 3 consecutive nodes; a final group with fewer than 3 nodes is left as it is. Which list of values results?

  1. A. [4, 3, 2, 1, 8, 7, 6, 5]
  2. B. [8, 7, 6, 5, 4, 3, 2, 1]
  3. C. [3, 2, 1, 6, 5, 4]
  4. D. [3, 2, 1, 6, 5, 4, 8, 7]

Correct answer: D. [3, 2, 1, 6, 5, 4, 8, 7]

Reverse each block of k independently, keeping the short tail unchanged. Reversing the whole list, or using a block size that does not match the request, gives a different order.

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

Exams that ask this topic