easyLinked ListRecursionLinked List

Merge Two Sorted Lists

easyTime: O(n + m)Space: O(1)

Recognize the pattern

merge two sorted sequenceslinked list nodesrecursive or iterative

Brute force idea

A straightforward first read of Merge Two Sorted Lists is this: Collect all values, sort, build new list. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

A calmer way to see Merge Two Sorted Lists is this: Compare heads, attach smaller one, advance that pointer. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Merge Two Sorted Lists is this: Always pick the smaller head and advance that list's pointer. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

A common way to get lost in Merge Two Sorted Lists is this: Forgetting the dummy head trick — a dummy node simplifies handling the first element. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Linked List Pattern