Merge Two Sorted Linked Lists Into One
Solve this Problemlist1 and list2, merge them into a single sorted list and return its head. The merged list should be made by re-using the nodes of the original two lists — no new node values.
Both inputs already arrive sorted, which is exactly the structure the optimal solution leans on: a two-pointer mergeTwo-Pointer MergeWalk two already-sorted sequences with one pointer each. At every step, the smaller of the two current elements is guaranteed to be the next-smallest element overall, so it can be appended immediately — no re-sorting, no lookahead. This is also the core building block behind merge sort. walks both lists side by side, always taking whichever current node holds the smaller value, and splices it directly into the result — turning two independent sorted chains into one sorted chain in a single O(n+m) pass with no extra nodes.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
0 ≤ number of nodes in list1, list2 ≤ 50 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.
| 1 | class Solution { |
| 2 | public Node mergeTwoSortedLists(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 | } |
| 19 |
dummyA dummy node lets the first spliced-in node attach the same way as every later one. tail starts at dummy.
Approach & Solutions
Brute Force — Collect Both Into an Array, Sort, Rebuild
GoodWalk both lists, copying every value into one array. Sort that array from scratch, then build a brand-new list from it. Correct, but it throws away the fact that list1 and list2 were each already sorted — re-sorting everything is unnecessary work, and building all-new nodes costs extra space the optimal solution doesn't need.
O((n+m) log(n+m))O(n+m)1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node mergeTwoSortedLists(Node list1, Node list2) {
9 List<Integer> vals = new ArrayList<>();
10 Node curr = list1;
11 while (curr != null) {
12 vals.add(curr.val);
13 curr = curr.next;
14 }
15 curr = list2;
16 while (curr != null) {
17 vals.add(curr.val);
18 curr = curr.next;
19 }
20 Collections.sort(vals);
21 Node dummy = new Node(0);
22 Node tail = dummy;
23 for (int v : vals) {
24 tail.next = new Node(v);
25 tail = tail.next;
26 }
27 return dummy.next;
28 }
29}Optimal — Two Pointers, Splice Nodes In Place
OptimalBoth lists are already sorted, so there's no need to look at every value twice. Walk list1 and list2 together with two pointers: at each 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 ever created — existing ones are just re-linked. Once one list runs out, the other's remaining nodes are already sorted, so the whole remainder attaches in one shot.
O(n+m)O(1)1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node mergeTwoSortedLists(Node list1, Node list2) {
9 Node dummy = new Node(0);
10 Node tail = dummy;
11 while (list1 != null && list2 != null) {
12 if (list1.val <= list2.val) {
13 tail.next = list1;
14 list1 = list1.next;
15 } else {
16 tail.next = list2;
17 list2 = list2.next;
18 }
19 tail = tail.next;
20 }
21 tail.next = (list1 != null) ? list1 : list2;
22 return dummy.next;
23 }
24}