notes/OJ notes/pages/Leetcode Merge-Two-Sorted-Lists.md

160 lines
3.1 KiB
Markdown
Raw Normal View History

2022-06-14 23:33:35 +08:00
# Leetcode Merge-Two-Sorted-Lists
#### 2022-06-14 22:57
---
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Algorithms:
2022-09-03 15:41:36 +08:00
2022-07-18 14:57:32 +08:00
#algorithm #two_pointers #recursion
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Data structures:
2022-09-03 15:41:36 +08:00
#DS #linked_list
2022-06-14 23:33:35 +08:00
##### Difficulty:
2022-09-03 15:41:36 +08:00
#leetcode #coding_problem #difficulty-easy
2022-06-14 23:33:35 +08:00
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
- [Link to problem](https://leetcode.com/problems/merge-two-sorted-lists/)
2022-09-03 15:41:36 +08:00
---
2022-06-14 23:33:35 +08:00
### Problem
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
You are given the heads of two sorted linked lists `list1` and `list2`.
Merge the two lists in a 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_.
#### Examples
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg)
```markdown
**Input:** list1 = [1,2,4], list2 = [1,3,4]
**Output:** [1,1,2,3,4,4]
```
**Example 2:**
```markdown
**Input:** list1 = [], list2 = []
**Output:** []
```
**Example 3:**
```markdown
**Input:** list1 = [], list2 = [0]
**Output:** [0]
```
#### Constraints
2022-09-03 15:41:36 +08:00
- 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.
2022-06-14 23:33:35 +08:00
### Thoughts
2022-07-18 14:57:32 +08:00
#### Two pointers method
2022-06-14 23:33:35 +08:00
This is a #two_pointers algorithm, I've done similar problems at leetcode's array list.
The only thing to watch out for is when there is one list remaining, remember to add the tails.
2022-07-18 14:57:32 +08:00
#### Recursion
Very simple for recursion
##### Base Case:
2022-09-03 15:41:36 +08:00
2022-07-18 14:57:32 +08:00
- Both are nullptr -> return nullptr
- One is nullptr -> return another
##### Pseudocode:
2022-09-03 15:41:36 +08:00
2022-07-18 14:57:32 +08:00
- Check for base case:
- if list1->val > list2->val, list2's next should be the merged result
- if list2->val > list1->val, list1's next should be the merged result.
2022-06-14 23:33:35 +08:00
### Solution
2022-09-03 15:41:36 +08:00
2022-07-18 14:57:32 +08:00
Recursion
2022-09-03 15:41:36 +08:00
2022-07-18 14:57:32 +08:00
```cpp
class Solution {
public:
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) {
// Recursion
// Base case, one is empty or both empty
if (!list1) {
return list2;
} else if (!list2) {
return list1;
}
if (list1->val > list2->val) {
// insert list1 after list2
list2->next = mergeTwoLists(list1, list2->next);
return list2;
} else {
list1->next = mergeTwoLists(list1->next, list2);
return list1;
}
}
};
```
Two pointers
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
```cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) {
// 2 Pointers, Space and time O(m + n);
ListNode *ptr1 = list1;
ListNode *ptr2 = list2;
ListNode *dummyHead = new ListNode();
ListNode *ptr3 = dummyHead;
while (ptr2 != NULL && ptr1 != NULL) {
if (ptr2->val <= ptr1->val) {
ptr3->next = ptr2;
ptr2 = ptr2->next;
} else {
ptr3->next = ptr1;
ptr1 = ptr1->next;
}
ptr3 = ptr3->next;
}
if (ptr2 == NULL) {
ptr3->next = ptr1;
} else if (ptr1 == NULL) {
ptr3->next = ptr2;
}
return dummyHead->next;
}
};
2022-09-03 15:41:36 +08:00
```