Merge a Pair of Ascending Linked Lists Recursively
Solve this Problemlist1 and list2, merge them into a single sorted linked list by splicing the existing nodes together — no new nodes should be created.
The recursive framing turns the merge into a chain of small decisions: compare the two current heads, let the smaller one lead, and set its `next` pointer to the result of recursively merging everything after it with the other list untouched. The base cases are immediate — merging anything with an empty list is just that list, unchanged. Every call makes exactly one decision and hands off a strictly smaller version of the same problem, so the total number of calls (and the total work) is proportional to the combined length of both lists — the same O(n+m) as the iterative two-pointer approach, just paying for it in call-stack frames instead of a fixed pair of pointers.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
0 ≤ number of nodes in list1, list2 ≤ 10 each - ◆
-100 ≤ node value ≤ 100 - ◆
Both list1 and list2 are sorted in non-decreasing order
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
Approach & Solutions
Iterative — Two Pointers, Splice Nodes In Place
OptimalWalk both lists together with two pointers. At every step, whichever current node holds the smaller value gets spliced onto the end of the result, and that list's pointer advances. No new nodes are created — existing ones are simply re-linked. Once one list runs out, the other's remaining nodes are already sorted, so the whole remainder attaches in one shot. This uses no extra memory beyond a couple of pointers, regardless of how long either list is.
O(n + m)O(1)1class Solution {
2 public Node mergeTwoSortedListsRecursive(Node list1, Node list2) {
3 Node dummy = new Node(0);
4 Node tail = dummy;
5 while (list1 != null && list2 != null) {
6 if (list1.val <= list2.val) {
7 tail.next = list1;
8 list1 = list1.next;
9 } else {
10 tail.next = list2;
11 list2 = list2.next;
12 }
13 tail = tail.next;
14 }
15 tail.next = (list1 != null) ? list1 : list2;
16 return dummy.next;
17 }
18}Recursive — Whichever Head Is Smaller Leads
GoodCompare the two current heads: whichever is smaller becomes the first node of the merged result, and its `next` pointer is set to the merged result of *the rest of its own list* combined with *all of the other list* — a smaller version of the exact same problem. The base cases are simple: merging anything with an empty list is just that non-empty list, unchanged. Every call picks exactly one node and recurses on what's left, so the total number of calls is proportional to the combined length of both lists.
O(n + m)O(n + m) call-stack space1class Solution {
2 public Node mergeTwoSortedListsRecursive(Node list1, Node list2) {
3 if (list1 == null) return list2;
4 if (list2 == null) return list1;
5 if (list1.val <= list2.val) {
6 list1.next = mergeTwoSortedListsRecursive(list1.next, list2);
7 return list1;
8 } else {
9 list2.next = mergeTwoSortedListsRecursive(list1, list2.next);
10 return list2;
11 }
12 }
13}