50 / 75
Easy
Merge Two Sorted Lists
#50linked-listrecursiontwo-pointers
Use dummy node and two pointers to build merged list by comparing and advancing smaller node.
Example:
Input:
l1=[1,2,4], l2=[1,3,4]Output:
[1,1,2,3,4,4]Common Mistakes:
- Not using dummy node (complicates head handling)
- Forgetting to append remaining nodes after one list ends
- Creating new nodes instead of reusing existing ones
- Off-by-one errors with pointer advancement
Notes:
Foundation for merge sort and merge k lists. Time O(n+m), Space O(1). Can also solve recursively.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Use dummy node and two pointers to build merged list by comparing and advancing smaller node.
Example:
Input:
l1=[1,2,4], l2=[1,3,4]Output:
[1,1,2,3,4,4]Common Mistakes:
- Not using dummy node (complicates head handling)
- Forgetting to append remaining nodes after one list ends
- Creating new nodes instead of reusing existing ones
- Off-by-one errors with pointer advancement
Notes:
Foundation for merge sort and merge k lists. Time O(n+m), Space O(1). Can also solve recursively.
50/75
Merge Two Sorted Lists