-
Notifications
You must be signed in to change notification settings - Fork 0
/
#21 MergeTwoSortedLists.cpp
60 lines (54 loc) · 1.59 KB
/
#21 MergeTwoSortedLists.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//Merge Two Sorted Lists
//https://leetcode.com/problems/merge-two-sorted-lists/
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* l1, ListNode* l2) {
ListNode* prev = nullptr;
ListNode* answer = nullptr;
while (l1 != nullptr || l2 != nullptr) {
ListNode* cur = new ListNode;
if (l1 == nullptr) {
cur->val = l2->val;
l2 = l2->next;
}
else if (l2 == nullptr) {
cur->val = l1->val;
l1 = l1->next;
}
else if (l1->val < l2->val) {
cur->val = l1->val;
l1 = l1->next;
}
else {
cur->val = l2->val;
l2 = l2->next;
}
if (prev == nullptr) {
answer = cur;
}
if (prev != nullptr) {
prev->next = cur;
}
prev = cur;
}
return answer;
}
};
int main() {
ListNode* a3 = new ListNode(4);
ListNode* a2 = new ListNode(2,a3);
ListNode* a1 = new ListNode(1, a2);
ListNode* b3 = new ListNode(4);
ListNode* b2 = new ListNode(3, b3);
ListNode* b1 = new ListNode(1, b2);
Solution sol;
auto res = sol.mergeTwoLists(a1, a2);
}