Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern

Two sorted linked lists [3 → 14 → 35 → 39] and [9 → 20 → 21 → 27 → 34] 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. 3 → 9 → 14 → 20 → 35 → 21 → 39 → 27 → 34
  2. B. 39 → 35 → 34 → 27 → 21 → 20 → 14 → 9 → 3
  3. C. 3 → 14 → 35 → 39 → 9 → 20 → 21 → 27 → 34
  4. D. 3 → 9 → 14 → 20 → 21 → 27 → 34 → 35 → 39

Correct answer: D. 3 → 9 → 14 → 20 → 21 → 27 → 34 → 35 → 39

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 [3 → 9 → 14 → 20 → 21 → 27 → 34 → 35 → 39] in one linear pass. Simply concatenating the two lists, [3 → 14 → 35 → 39 → 9 → 20 → 21 → 27 → 34], is not sorted.

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

Exams that ask this topic