#21 Merge Two Sorted Lists
frequently reported atBloombergAmazonMicrosoftGoogleAppleMeta
the problem
In plain words
Merge two sorted linked lists into one sorted list by splicing their nodes together. Return the head of the merged list.
in list1 = [1,2,4], list2 = [1,3,4]
out [1,1,2,3,4,4]
in list1 = [], list2 = []
out []
in list1 = [], list2 = [0]
out [0]
- 0 ≤ nodes in each list ≤ 50
- -100 ≤ Node.val ≤ 100
- Both lists are sorted in non-decreasing order
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternMerge sort & merging sorted runsIn-place linked-list rewiring
The smallest remaining node is always one of the two current heads. Take it, re-link it after tail, repeat. A dummy head avoids special-casing the first pick.
solutions
From brute force to optimal
Dump all values into a list, sort, build new nodes.
- Copy values from both lists.
- Sort.
- Build a new list.
Sorting ignores that the inputs were already sorted.
A value list plus brand-new nodes.
class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { List<Integer> values = new ArrayList<>(); for (ListNode p = list1; p != null; p = p.next) values.add(p.val); for (ListNode p = list2; p != null; p = p.next) values.add(p.val); Collections.sort(values); ListNode dummy = new ListNode(); ListNode tail = dummy; for (int v : values) { tail.next = new ListNode(v); tail = tail.next; } return dummy.next; }}The smaller head wins; its next is the merge of everything else.
- If one list is empty, return the other.
- Smaller head.next = merge(rest).
- Return the smaller head.
One call per node.
Recursion depth equals the total length.
class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { if (list1 == null) return list2; if (list2 == null) return list1; if (list1.val <= list2.val) { list1.next = mergeTwoLists(list1.next, list2); // smaller head wins, merge the rest return list1; } list2.next = mergeTwoLists(list1, list2.next); return list2; }}Walk both lists; attach the smaller node to tail; attach the leftover list at the end.
- dummy, tail = dummy.
- While both non-empty: tail.next = smaller node; advance that list; tail = tail.next.
- tail.next = whichever list remains.
- Return dummy.next.
Each loop step places one node.
Re-links existing nodes; only dummy is new.
class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { ListNode dummy = new ListNode(); ListNode tail = dummy; while (list1 != null && list2 != null) { if (list1.val <= list2.val) { tail.next = list1; list1 = list1.next; } else { tail.next = list2; list2 = list2.next; } tail = tail.next; } tail.next = (list1 != null) ? list1 : list2; return dummy.next; }}Dry run
in the room
How to say it in the interview
talk track
- Mention the dummy node up front and why: the head of the result isn't known yet.
- Point out the final
tail.next = list1 != null ? list1 : list2— the leftover is already sorted, so no loop is needed. - Use
<=so equal values keep their original order (stable). - If asked for recursion: "the smaller head is first; the rest is a smaller merge" — six lines, but O(n + m) stack.
traps
- Forgetting to attach the leftover list after the loop — the tail of the longer list is lost.
- Using
<instead of<=is still correct, but<=keeps equal values stable (list1 first).
what they ask next
Follow-ups
Merge k Sorted Lists (LC 23)?
Min-heap of the k current heads → O(N log k). Or merge pairs in rounds (divide and conquer), also O(N log k).
Sort List (LC 148) in O(n log n)?
Merge sort: split with slow/fast, sort each half recursively, merge with this exact routine — it is merge sort's merge step.
before you submit
Edge cases to test
- One or both lists empty.
- Equal values in both lists — take list1's first to stay stable.
- One list entirely smaller than the other — the loop ends early and the rest is attached in one step.
practise next
Similar problems
- #23Merge k Sorted ListsHardin Heaps
- #148Sort ListMediumin Sorting
- #86Partition ListMediumin Linked Lists