
Problem
Solution
Submissions
Merge Two Sorted Linked Lists
Certification: Advanced Level
Accuracy: 50%
Submissions: 4
Points: 8
Write a C# function to merge two sorted linked lists and return the merged list. The new list should be made by splicing together the nodes of the first two lists and should also be sorted.
Example 1
- Input: list1 = [1, 2, 4], list2 = [1, 3, 4]
- Output: [1, 1, 2, 3, 4, 4]
- Explanation:
- The two lists are merged in ascending order.
Example 2
- Input: list1 = [], list2 = [0]
- Output: [0]
- Explanation:
- The first list is empty, so the result is just the second list.
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
- Time Complexity: O(n + m) where n and m are the lengths of the two lists
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a dummy head node to simplify edge cases
- Compare the values of both list heads and add the smaller one to the result
- Advance the pointer in the list that provided the smaller value
- Handle cases where one list becomes empty before the other