Last Updated: March 29, 2026
We have two linked lists, each already sorted in non-decreasing order, and we need to combine them into a single sorted linked list. The key phrase is "splicing together the nodes," which means we should reuse the existing nodes rather than creating new ones.
If you think about how you'd merge two sorted piles of cards by hand, the process is intuitive: look at the top card of each pile, pick the smaller one, place it on the output pile, and repeat. When one pile runs out, just move the remaining pile directly onto the output. That's exactly the algorithm here. The only wrinkle is that we're working with linked list pointers instead of arrays, so we need to carefully wire up .next pointers as we go.
0 <= number of nodes <= 50 → Both lists can be empty (zero nodes). Any approach, even O(n^2), would be instant with n up to 50. Performance is not a concern here, so focus on clean, correct logic.-100 <= Node.val <= 100 → Values can be negative. The comparison logic works the same regardless.Both lists are sorted in non-decreasing order → This is the critical property that makes a simple merge possible. We can always compare just the current heads, because the smallest unseen element in each list is always at the front.Here's the recursive insight: at each step, we compare the heads of the two lists. Whichever is smaller becomes the head of the merged result. Then its .next should point to whatever comes from merging the rest of that list with the other list. That's the same problem, just smaller.
The base cases are simple. If one list is empty, the answer is the other list. If both are empty, the answer is null.
This is a natural way to think about the problem, and the code is elegant. But it uses O(n) stack space due to recursion, which matters if the lists are very long.
list1 is null, return list2. If list2 is null, return list1.list1 and list2.list1.val <= list2.val, set list1.next to the result of merging list1.next with list2. Return list1.list2.next to the result of merging list1 with list2.next. Return list2.The recursive approach is clean but uses O(n + m) stack space. Can we do the same merge iteratively with only O(1) extra space?
The idea is the same as the recursive approach: compare heads, pick the smaller one, advance that pointer. But instead of using the call stack to remember where we are, we maintain a tail pointer that tracks the end of the merged list we're building.
There's one annoying detail with linked list problems: the very first node needs special handling because there's no "previous node" to attach it to. A common trick is to create a dummy node at the start. The dummy's .next will eventually point to the real head of the merged list. This eliminates the special case for the first insertion and makes the code much cleaner.
Both input lists are sorted. At every step, the smallest remaining element across both lists is guaranteed to be one of the two current heads. By always picking the smaller head and advancing that pointer, we build the merged list in sorted order. When one list is exhausted, the other list's remaining elements are already sorted and all greater than or equal to the last element we placed, so we can attach the entire remainder in one operation.
tail pointer to the dummy.list1 and list2 are non-null:list1.val and list2.val.tail.next, then advance that list's pointer.tail to tail.next.tail.next.dummy.next (the real head of the merged list).