Merge two sorted lists
Standard · Write code · about 12 min
Given the heads of two sorted singly linked lists l1 and l2, merge them into one sorted list by relinking their nodes, and return its head.
Example 1
Input:
[[1,4,6],[2,3,7]]
Output:
[1,2,3,4,6,7]
Example 2
Input:
[[],[0]]
Output:
[0]
One list is empty: the answer is the other list.
Constraints
- 0 ≤ length of each list ≤ 5,000
- -10,000 ≤ node value ≤ 10,000
- Both lists are sorted in non-decreasing order.
Learn the idea first: Linked list techniques: reverse, middle, merge, detect a cycle, a visual lesson.
More Linked list techniques: reverse, middle, merge, detect a cycle problems
- Reverse a list Warm-up
- Middle node Warm-up
- Palindrome list Standard
- Trace a list reversal Standard
- Find the cycle start Challenge