Practice Discrete Math

Algorithms / Linked List Sorted Merge

Least You Need to Know: Merging Sorted Lists and Splicing Runs

Sorted linked-list merge is a classic interview pattern because the lists are already ordered, so you can advance exactly one front pointer at a time. The algorithm is linear in the total number of nodes and is often implemented cleanly with a dummy head.

The least you need to know

Key notation

l1, l2 current fronts of the two sorted lists
tail end of the merged output so far
m + n total nodes processed across both lists

Tiny worked example

  • Compare the heads of the two lists.
  • Attach the smaller node to the merged output and advance that list only.
  • Repeat until one list is empty.
  • Then attach the untouched remainder of the other list in one step.

Common mistakes

How to recognize this kind of problem

Start practice