Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern

Two sorted linked lists [9 → 14 → 35 → 36 → 37] and [27 → 29 → 31 → 32] are merged with the standard O(n+m) two-pointer merge (always take the smaller head, splicing nodes, no new array). What is the merged list?

  1. A. 9 → 27 → 14 → 29 → 35 → 31 → 36 → 32 → 37
  2. B. 9 → 14 → 27 → 29 → 31 → 32 → 35 → 36 → 37
  3. C. 9 → 14 → 35 → 36 → 37 → 27 → 29 → 31 → 32
  4. D. 37 → 36 → 35 → 32 → 31 → 29 → 27 → 14 → 9

Correct answer: B. 9 → 14 → 27 → 29 → 31 → 32 → 35 → 36 → 37

At each step the merge compares the current heads of both lists and splices the smaller one onto the result, advancing only that list's pointer; this produces the fully sorted list [9 → 14 → 27 → 29 → 31 → 32 → 35 → 36 → 37] in one linear pass. Simply concatenating the two lists, [9 → 14 → 35 → 36 → 37 → 27 → 29 → 31 → 32], is not sorted.

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

Exams that ask this topic