Merge Two Sorted Lists - Problem

Given the heads of two sorted linked lists list1 and list2, merge them into a single sorted linked list.

The merged list should be constructed by splicing together the nodes from the input lists - don't create new nodes, just rearrange the existing ones. Return the head of the newly merged linked list.

Example:
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

This is a classic linked list problem that tests your understanding of pointer manipulation and merging algorithms. It's the foundation for more complex problems like merging k sorted lists.

Input & Output

example_1.py โ€” Basic Merge
$ Input: list1 = [1,2,4], list2 = [1,3,4]
โ€บ Output: [1,1,2,3,4,4]
๐Ÿ’ก Note: Both lists start with 1, so we take from list1 first. Then we compare 2 vs 1 (from list2), take 1. Continue this process: 2 vs 3 (take 2), 4 vs 3 (take 3), 4 vs 4 (take from list1), then append remaining 4 from list2.
example_2.py โ€” Empty List
$ Input: list1 = [], list2 = [0]
โ€บ Output: [0]
๐Ÿ’ก Note: When one list is empty, we simply return the other list. No comparison needed.
example_3.py โ€” Both Empty
$ Input: list1 = [], list2 = []
โ€บ Output: []
๐Ÿ’ก Note: When both lists are empty, the result is also an empty list.

Visualization

Tap to expand
Merging Two Sorted LinesLine 1 (Heights):5'1"5'2"5'4"Line 2 (Heights):5'1"5'3"5'4"comparingcomparingDecision Process:1. Compare front students: 5'1" vs 5'1" โ†’ Equal, take from Line 12. Move to merged line3. Advance pointer in Line 1Merged Line (Result):5'1"5'1"5'2"5'3"5'4"5'4"โœ“ Perfectly sorted from shortest to tallest!Algorithm Steps:while both lines have people:โ€ข Compare front studentsโ€ข Move shorter one to resultโ€ข Advance that line's pointerwhen one line is empty:โ€ข Move all remaining students from other line to result
Understanding the Visualization
1
Setup
Start with two sorted lines and an empty result line
2
Compare
Look at the first person in each line
3
Choose
Move the shorter person to the result line
4
Repeat
Continue until one line is empty
5
Finish
Move all remaining people from the non-empty line
Key Takeaway
๐ŸŽฏ Key Insight: Since both input lists are already sorted, we only need to compare the first elements and choose the smaller one at each step. This gives us O(n+m) time with O(1) space complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Visit each node exactly once where n and m are lengths of the lists

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only uses a constant amount of extra space for pointers

n
2n
โœ“ Linear Space

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
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 22
142.4K Views
Very High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen