Merge Two Sorted Lists
Easy · Linked lists
Jump to codeCombine two ascending singly linked lists into one ascending list by relinking their nodes. Either input may be empty.
Examples
Example 1
list1 = [1,2,4] list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2
list1 = [] list2 = []
Output: []
Constraints
- The number of nodes in both lists is in the range [0, 50].
- -100 <= Node.val <= 100
- Both list1 and list2 are sorted in non-decreasing order.
Function contract
Linked lists are displayed as value arrays. Your code receives ListNode objects with val and next fields.
Need a hint?
A dummy head removes the special case for the first output node.
Asked in real interviews
Candidates reported versions of Merge Two Sorted Lists from these company interviews. Some company questions are locked and open with Premium or XP.
- Merge two sorted linked listsTwo Sigma
- Merge Two Sorted Singly Linked ListsNetapp
- Merge two sorted linked listsHubSpot
Solution approach
Maintain a tail behind a dummy node. Repeatedly attach the smaller input head and advance that input. Advance tail after each attachment. Once one input is exhausted, attach the other remainder and return dummy.next.
Complexity
O(m + n) time and O(1) extra space.
Python reference solution
def mergeTwoLists(list1, list2):
dummy = ListNode()
tail = dummy
while list1 and list2:
if list1.val <= list2.val:
tail.next, list1 = list1, list1.next
else:
tail.next, list2 = list2, list2.next
tail = tail.next
tail.next = list1 or list2
return dummy.next
JavaScript solution
function mergeTwoLists(list1, list2) {
const dummy = new ListNode();
let tail = dummy;
while (list1 && list2) {
if (list1.val <= list2.val) {
tail.next = list1; list1 = list1.next;
} else {
tail.next = list2; list2 = list2.next;
}
tail = tail.next;
}
tail.next = list1 || list2;
return dummy.next;
}
Java solution
import java.util.*;
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new 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;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy;
ListNode* tail = &dummy;
while (list1 && list2) {
if (list1->val <= list2->val) {
tail->next = list1; list1 = list1->next;
} else {
tail->next = list2; list2 = list2->next;
}
tail = tail->next;
}
tail->next = list1 ? list1 : list2;
return dummy.next;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 8 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
1,
2,
4
],
[
1,
3,
4
]
][ 1, 1, 2, 3, 4, 4 ]
[ [], [] ]
[]
[
[],
[
0
]
][ 0 ]
[
[
2,
4,
6
],
[
1,
3,
5
]
][ 1, 2, 3, 4, 5, 6 ]
[
[
1,
1,
2
],
[
1,
3
]
][ 1, 1, 1, 2, 3 ]
Drafts are saved per language in this browser when storage is available.