Merge Two Sorted Lists in Java: Dummy Node, Recursion, and the Collect-and-Sort Idea
-
Last Updated: November 26, 2026
-
By: javahandson
-
Series

Learn to merge two sorted lists in Java with three approaches — collect-and-sort, recursion, and the iterative dummy node — each with a step-by-step dry run.
Merge two sorted lists in Java is a classic linked list warm-up. It teaches a pattern you will reuse again and again. You get two linked lists that are already sorted. Your job is to join them into one list that stays sorted.
The task sounds small, yet it packs a nice lesson. Because both lists come pre-sorted, you can merge them by always picking the smaller head first. No extra sorting is needed once you spot that trick.
We build the answer in three steps, as always. The collect-and-sort idea is the easy first try. Recursion is the elegant next step. The iterative dummy node is the lean version interviewers love to see.
Every approach gets a full, step-by-step dry run on the same two lists. Nothing gets skipped, so you can watch each pointer move and see exactly how the merged list grows node by node.
Let us pin down the rules before we touch any code.
For our pair the merged list runs 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14. Notice how the two lists interleave perfectly, which makes the dry runs easy to follow.
A linked list is a chain of nodes. Every node stores a value and a pointer to the next node. The last node points to null, which marks the end.
A dummy node is a throwaway node you place before the real head. It gives you a fixed starting point to attach nodes to. Later you just return dummy.next, which is the true head of the merged list.
Since both lists are sorted, the smallest value overall is always at one of the two heads. So you compare the two heads, pick the smaller one, and move that list’s pointer forward. Repeat until one list runs out.
Walk both lists and dump every value into an array. Sort that array. Then build a brand new linked list from the sorted values. This idea ignores the fact that the lists are already sorted. So it does more work than needed, yet it is the simplest thing to reach for first.
values = empty list
node = list1
while node is not null:
values.add(node.value)
node = node.next
node = list2
while node is not null:
values.add(node.value)
node = node.next
sort(values)
dummy = new node
tail = dummy
for v in values:
tail.next = new node(v)
tail = tail.next
return dummy.nextimport java.util.*;
public class MergeCollectSort {
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
List<Integer> values = new ArrayList<>();
for (ListNode node = list1; node != null; node = node.next) {
values.add(node.val);
}
for (ListNode node = list2; node != null; node = node.next) {
values.add(node.val);
}
Collections.sort(values);
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
for (int v : values) {
tail.next = new ListNode(v);
tail = tail.next;
}
return dummy.next;
}
}We use list1 = 1 → 3 → 5 → 7 → 9 → 11 → 13 and list2 = 2 → 4 → 6 → 8 → 10 → 12 → 14. The collect pass walks list1 first, then list2, adding each value to the array in the order it is read.
| Reading | value read | array after this step |
|---|---|---|
| list1 node 1 | 1 | [1] |
| list1 node 2 | 3 | [1, 3] |
| list1 node 3 | 5 | [1, 3, 5] |
| list1 node 4 | 7 | [1, 3, 5, 7] |
| list1 node 5 | 9 | [1, 3, 5, 7, 9] |
| list1 node 6 | 11 | [1, 3, 5, 7, 9, 11] |
| list1 node 7 | 13 | [1, 3, 5, 7, 9, 11, 13] |
| list2 node 1 | 2 | [1, 3, 5, 7, 9, 11, 13, 2] |
| list2 node 2 | 4 | [1, 3, 5, 7, 9, 11, 13, 2, 4] |
| list2 node 3 | 6 | [1, 3, 5, 7, 9, 11, 13, 2, 4, 6] |
| list2 node 4 | 8 | [1, 3, 5, 7, 9, 11, 13, 2, 4, 6, 8] |
| list2 node 5 | 10 | [1, 3, 5, 7, 9, 11, 13, 2, 4, 6, 8, 10] |
| list2 node 6 | 12 | [1, 3, 5, 7, 9, 11, 13, 2, 4, 6, 8, 10, 12] |
| list2 node 7 | 14 | [1, 3, 5, 7, 9, 11, 13, 2, 4, 6, 8, 10, 12, 14] |
After collecting, the array holds all of list1 followed by all of list2. Now Collections.sort rearranges it into increasing order.
| Stage | array |
|---|---|
| Before sort | [1, 3, 5, 7, 9, 11, 13, 2, 4, 6, 8, 10, 12, 14] |
| After sort | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
With the array sorted, the rebuild pass creates one node per value and links them in a row. The tail pointer always points to the last node added.
| Step | value | merged list after attaching |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 1 → 2 |
| 3 | 3 | 1 → 2 → 3 |
| 4 | 4 | 1 → 2 → 3 → 4 |
| 5 | 5 | 1 → 2 → 3 → 4 → 5 |
| 6 | 6 | 1 → 2 → 3 → 4 → 5 → 6 |
| 7 | 7 | 1 → 2 → 3 → 4 → 5 → 6 → 7 |
| 8 | 8 | 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 |
| 9 | 9 | 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 |
| 10 | 10 | 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 |
| 11 | 11 | … → 10 → 11 |
| 12 | 12 | … → 11 → 12 |
| 13 | 13 | … → 12 → 13 |
| 14 | 14 | … → 13 → 14 (complete) |
The method returns dummy.next, so the final merged list is 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14.
Let us walk the three passes and see what each one does.
Collect pass over list1, then list2.
So the array ends up as list1 glued in front of list2. It is clearly out of order, because 13 sits right before 2. That is fine, because the sort step fixes it.
Sort pass.
Collections.sort takes the jumbled array and lines every value up from smallest to largest. The stray 2 that was stuck after 13 jumps forward to sit right after 1. After sorting, the array is a clean 1 through 14.
Rebuild pass.
When the loop finishes, the chain runs 1 → 2 → … → 14. Returning dummy.next hands back the node holding 1, which is the real head.
| 💡 Interview Insight A common follow-up is “does this use the sorted input?” Be honest: it throws the sorted order away and re-sorts, so it wastes the gift the problem gave you. Interviewers expect you to point this out yourself. |
This works, but it is heavier than it needs to be. Both lists arrive sorted already, so re-sorting is wasted effort. Next we use that sorted property directly.
Compare the two heads. Pick the smaller one. Then say its next node is the merge of the rest. That one sentence is the whole recursion. The base case is simple: if either list is empty, return the other list as is.
merge(a, b):
if a is null:
return b
if b is null:
return a
if a.value <= b.value:
a.next = merge(a.next, b)
return a
else:
b.next = merge(a, b.next)
return bpublic class MergeRecursive {
public static ListNode mergeTwoLists(ListNode a, ListNode b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
if (a.val <= b.val) {
a.next = mergeTwoLists(a.next, b);
return a;
} else {
b.next = mergeTwoLists(a, b.next);
return b;
}
}
}We merge the same two lists. Each row is one call to mergeTwoLists. The indent shows how deep the call stack is. First the stack goes down, picking one node per call, until a list runs empty.
| Call depth | a head | b head | compare | pick → recurse on |
|---|---|---|---|---|
| 0 | 1 | 2 | 1 ≤ 2 | a=1 → merge(3…, 2…) |
| 1 | 3 | 2 | 3 > 2 | b=2 → merge(3…, 4…) |
| 2 | 3 | 4 | 3 ≤ 4 | a=3 → merge(5…, 4…) |
| 3 | 5 | 4 | 5 > 4 | b=4 → merge(5…, 6…) |
| 4 | 5 | 6 | 5 ≤ 6 | a=5 → merge(7…, 6…) |
| 5 | 7 | 6 | 7 > 6 | b=6 → merge(7…, 8…) |
| 6 | 7 | 8 | 7 ≤ 8 | a=7 → merge(9…, 8…) |
| 7 | 9 | 8 | 9 > 8 | b=8 → merge(9…, 10…) |
| 8 | 9 | 10 | 9 ≤ 10 | a=9 → merge(11…, 10…) |
| 9 | 11 | 10 | 11 > 10 | b=10 → merge(11…, 12…) |
| 10 | 11 | 12 | 11 ≤ 12 | a=11 → merge(13…, 12…) |
| 11 | 13 | 12 | 13 > 12 | b=12 → merge(13…, 14…) |
| 12 | 13 | 14 | 13 ≤ 14 | a=13 → merge(null, 14…) |
| 13 | null | 14 | a is null | base case → return 14 |
The deepest call hits the base case and returns the node 14. Now the stack unwinds. Each waiting call sets the next pointer of the node it picked, from the bottom back up to the top.
| Unwind at depth | node that was picked | its next is set to | chain built so far |
|---|---|---|---|
| 12 | 13 | 14 | 13 → 14 |
| 11 | 12 | 13 | 12 → 13 → 14 |
| 10 | 11 | 12 | 11 → 12 → 13 → 14 |
| 9 | 10 | 11 | 10 → 11 → 12 → 13 → 14 |
| 8 | 9 | 10 | 9 → 10 → 11 → 12 → 13 → 14 |
| 7 | 8 | 9 | 8 → 9 → … → 14 |
| 6 | 7 | 8 | 7 → 8 → … → 14 |
| 5 | 6 | 7 | 6 → 7 → … → 14 |
| 4 | 5 | 6 | 5 → 6 → … → 14 |
| 3 | 4 | 5 | 4 → 5 → … → 14 |
| 2 | 3 | 4 | 3 → 4 → … → 14 |
| 1 | 2 | 3 | 2 → 3 → … → 14 |
| 0 | 1 | 2 | 1 → 2 → … → 14 (final head) |
When the top call returns, it hands back node 1, and the full list is 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14.
The recursion has two halves: the dive down and the climb back up. Let us read both.
The dive: picking one node per call.
At depth 13 the first list is null. The base case fires and returns node 14 untouched. That return value is the tail of our merged list.
The climb: wiring up the next pointers.
So the list is actually stitched together on the way back up. The picks happen going down, but the next pointers get wired going up. The top call then returns node 1 as the new head.
| 💡 Interview Insight If asked “what is the catch with recursion here?”, mention the call stack. One level of recursion is used per node, so a very long list can overflow the stack. The iterative version coming next avoids that risk. |
Recursion reads cleanly and uses the sorted order well. The one weak spot is that call stack. So next we do the same merge with a simple loop and only a few pointers.
Keep a dummy node and a tail pointer. Compare the two current heads, attach the smaller one to the tail, and move that pointer forward. Repeat until one list is empty, then attach whatever remains. This reuses the original nodes, so no new list is built. It is fast, lean, and the answer interviewers hope to see.
dummy = new node
tail = dummy
lower = list1 // current head of list1
higher = list2 // current head of list2
while lower is not null and higher is not null:
if lower.value <= higher.value:
tail.next = lower
lower = lower.next
else:
tail.next = higher
higher = higher.next
tail = tail.next
if lower is not null:
tail.next = lower
else:
tail.next = higher
return dummy.nextpublic class MergeIterative {
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
ListNode lower = list1;
ListNode higher = list2;
while (lower != null && higher != null) {
if (lower.val <= higher.val) {
tail.next = lower;
lower = lower.next;
} else {
tail.next = higher;
higher = higher.next;
}
tail = tail.next;
}
tail.next = (lower != null) ? lower : higher;
return dummy.next;
}
}We merge list1 = 1 → 3 → 5 → 7 → 9 → 11 → 13 and list2 = 2 → 4 → 6 → 8 → 10 → 12 → 14. The legend: “lower head” is the current node of list1, “higher head” is the current node of list2. Each step attaches one node to the tail and advances one pointer.
| step | lower head (list1) | higher head (list2) | compare | attach | tail chain after step |
|---|---|---|---|---|---|
| 1 | 1 | 2 | 1 ≤ 2 | lower 1 | 1 |
| 2 | 3 | 2 | 3 > 2 | higher 2 | 1 → 2 |
| 3 | 3 | 4 | 3 ≤ 4 | lower 3 | 1 → 2 → 3 |
| 4 | 5 | 4 | 5 > 4 | higher 4 | 1 → 2 → 3 → 4 |
| 5 | 5 | 6 | 5 ≤ 6 | lower 5 | 1 → 2 → 3 → 4 → 5 |
| 6 | 7 | 6 | 7 > 6 | higher 6 | 1 → 2 → 3 → 4 → 5 → 6 |
| 7 | 7 | 8 | 7 ≤ 8 | lower 7 | … → 6 → 7 |
| 8 | 9 | 8 | 9 > 8 | higher 8 | … → 7 → 8 |
| 9 | 9 | 10 | 9 ≤ 10 | lower 9 | … → 8 → 9 |
| 10 | 11 | 10 | 11 > 10 | higher 10 | … → 9 → 10 |
| 11 | 11 | 12 | 11 ≤ 12 | lower 11 | … → 10 → 11 |
| 12 | 13 | 12 | 13 > 12 | higher 12 | … → 11 → 12 |
| 13 | 13 | 14 | 13 ≤ 14 | lower 13 | … → 12 → 13 |
After step 13, list1 is exhausted: lower is now null, while higher still points at node 14. So the loop stops.
| Loop exit state | value |
|---|---|
| lower (list1) | null — list1 fully used |
| higher (list2) | 14 — one node left |
| attach leftover | tail.next = higher, so 14 joins the end |
| final merged list | 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14 |
Let us step through the loop and watch the two pointers march forward.
Step 1, heads 1 and 2.
The tail now sits on node 1, and the merged chain reads just 1.
Step 2, heads 3 and 2.
The chain grows to 1 → 2. Notice the pick flipped from list1 to list2.
Steps 3 to 6, the heads keep swapping.
Because our lists interleave as odd then even, the winner keeps alternating. So the merged chain climbs 1 → 2 → 3 → 4 → 5 → 6 in neat order.
Steps 7 to 13, climbing to the end.
With lower now null, list1 is fully used up. The loop condition fails, so we leave the loop.
Attaching the leftover.
Here that leftover is just node 14, but if list2 had several nodes left, they would all join at once because they are already sorted. Returning dummy.next gives the head node 1, and the full list is 1 → 2 → … → 14.
| 💡 Interview Insight Interviewers often ask “why the dummy node?” Explain that it removes the special case for the first node. Without it, you would need an extra check to set the head separately, which clutters the loop. |
| Approach | How it merges | Extra memory | Speed feel |
|---|---|---|---|
| Collect and sort | Dump values, sort, rebuild | New array + new list | Slowest of the three |
| Recursion | Pick smaller head, recurse | Call stack, one frame per node | Fast, but stack-heavy |
| Iterative dummy | Loop, attach smaller, reuse nodes | A few pointers only | Fastest and leanest |
All three return the same merged list. They just pay different prices to get there.
| Approach | Time | Space | Note |
|---|---|---|---|
| Collect and sort | O(n log n) | O(n) | Simple, but re-sorts already sorted input |
| Recursion | O(n) | O(n) | Elegant, but uses stack depth n |
| Iterative dummy node | O(n) | O(1) | Fast and lean, the expected answer |
The two linear approaches both visit each node once. What sets them apart is memory. Recursion needs a stack frame per node, while the iterative version uses only a handful of pointers, so it stays at O(1) extra space.
In an interview, mention collect-and-sort first to show you see the easy route. Then move to the iterative dummy node as your real answer. Keep recursion in your pocket as the clean, short alternative when stack depth is not a worry.
| 💡 Interview Insight If pushed on the O(1) space claim for the iterative version, note that it reuses the original nodes instead of creating new ones. Only a fixed set of pointers is added, no matter how long the lists are. |
A few small traps catch beginners on this problem. Keep them in mind.
Run the empty-list case and a single-node case through your code before you call it done. Those two catch more bugs than any long input will.
A: The iterative dummy node approach is the most efficient. It runs in O(n) time and O(1) extra space because it reuses the original nodes instead of building a new list. You compare the two heads, attach the smaller node to the tail, and attach the leftover list at the end.
A: The dummy node gives the tail pointer a fixed starting point, so the first real node is attached the same way as every other node. Without it, you would need extra code just to set the head of the merged list, which clutters the loop.
A: Both run in O(n) time. Recursion is clean and short but uses one stack frame per node, so a very long list can overflow the stack. Iteration with a dummy node uses only a few pointers and stays at O(1) extra space, so it is the safer pick for large inputs and the answer interviewers usually expect.
A: If one list is empty, the merged result is simply the other list. The recursive version handles this with its base case, and the iterative version handles it with the final leftover-attach step, so no special head-setting code is needed.
A: That works, but it wastes the fact that both lists already arrive sorted. Collecting and re-sorting costs O(n log n) time and O(n) extra space, while a direct merge is O(n). Mention it in an interview to show you see the easy route, then move to the dummy-node merge.
Merge two sorted lists in Java looks tiny, yet it teaches a habit you will reuse everywhere: when inputs arrive sorted, merge them instead of re-sorting. Once you see that, the problem almost solves itself.
Our seven-node lists showed the payoff clearly. Collect-and-sort worked but wasted the sorted order. Recursion used that order with a clean two-line idea. The iterative dummy node did the same merge with only a few pointers and no extra list.
So take the pattern, not just the answer. Reach for the dummy node to kill edge cases. Compare heads, attach the smaller, and remember to tack on the leftover at the end.
That merge habit shows up again in merge sort, in merging k lists, and in many range problems. It will serve you well on the harder problems waiting further down the list.