Data Structures & AlgorithmsVery hardProduct company OA

Two sorted linked lists [12 → 17 → 24 → 26 → 32] and [8 → 10 → 12 → 38] 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. 12 → 17 → 24 → 26 → 32 → 8 → 10 → 12 → 38
  2. B. 38 → 32 → 26 → 24 → 17 → 12 → 12 → 10 → 8
  3. C. 8 → 10 → 12 → 12 → 17 → 24 → 26 → 32 → 38
  4. D. 12 → 8 → 17 → 10 → 24 → 12 → 26 → 38 → 32

Correct answer: C. 8 → 10 → 12 → 12 → 17 → 24 → 26 → 32 → 38

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 [8 → 10 → 12 → 12 → 17 → 24 → 26 → 32 → 38] in one linear pass. Simply concatenating the two lists, [12 → 17 → 24 → 26 → 32 → 8 → 10 → 12 → 38], is not sorted.

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

Exams that ask this topic