Merge Two Sorted Lists
You get two linked lists, each sorted from small to large. Join them into one sorted list made of their own nodes and return its head. Our example: 3 → 6 → 11 and 1 → 6 → 8 — the answer is 1 → 3 → 6 → 6 → 8 → 11.
- input
list1,list2: the heads of two linked lists, each sorted in non-decreasing order; either may be empty.- returns
- The head of one sorted list made of the nodes of both, spliced together.
- constraints
- 0 ≤ nodes in each list ≤ 50
- −100 ≤ Node.val ≤ 100
- list1 and list2 are sorted in non-decreasing order
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
function mergeTwoLists(list1, list2) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more linked list pointers problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.