21. Merge Two Sorted Lists
You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Example 1:
Example 2:
Example 3:
- 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.
Constraints:
Notes
Intuition
Since both lists are sorted, we can build the merged list by always taking the smaller of the two current nodes. This is similar to the merge step in merge sort.
Implementation
Create a dummy node to simplify head handling and a curr pointer for building the result. While both lists have nodes, compare their values—append the smaller one to the result and advance that list's pointer. After the main loop, one list may have remaining nodes; append them directly since they're already sorted.
Edge-cases
If either list is initially empty, the other list is returned as-is. The dummy node eliminates special case handling for the first element.
- Time: O(m + n) — each node visited once
- Space: O(1) — reusing existing nodes (or O(m + n) if creating new nodes)