-
Notifications
You must be signed in to change notification settings - Fork 8
/
leetcode21-merge-two-sorted-lists.cpp
128 lines (103 loc) · 2.67 KB
/
leetcode21-merge-two-sorted-lists.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @file leetcode21-merge-two-sorted-lists.cpp
* @author wangguibao(wang_guibao@163.com)
* @date 2022/07/27 16:40:46
* @brief https://leetcode.com/problems/merge-two-sorted-lists/
**/
#include <iostream>
#include <vector>
/**
* 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) {
ListNode* head = new ListNode();
ListNode* p = head;
ListNode* p1 = list1;
ListNode* p2 = list2;
while (p1 && p2) {
if (p1->val <= p2->val) {
p->next = p1;
p1 = p1->next;
} else {
p->next = p2;
p2 = p2->next;
}
p = p->next;
}
if (p1) {
p->next = p1;
} else if (p2) {
p->next = p2;
}
return head->next;
}
};
ListNode* create_list_from_vec(std::vector<int> nums) {
ListNode* head = nullptr;
ListNode* cur = nullptr;
for (auto x: nums) {
ListNode* node = new ListNode(x);
if (head == nullptr) {
head = node;
cur = head;
} else {
cur->next = node;
cur = cur->next;
}
}
return head;
}
void print_list(ListNode* head) {
ListNode* p = head;
std::cout << "[";
while(p) {
std::cout << p->val << " ";
p = p->next;
}
std::cout << "]" << std::endl;
}
int main() {
while (1) {
std::cout << "Number of elements for first list (negative exits): " << std::endl;
int n;
std::cin >> n;
if (n < 0) {
return 0;
}
std::cout << n << " sorted integers: ";
std::vector<int> l1;
int x;
for (size_t i = 0; i < n; ++i) {
std::cin >> x;
l1.push_back(x);
}
std::cout << "Number of elements for second list (negative exits): " << std::endl;
std::cin >> n;
if (n < 0) {
return 0;
}
std::cout << n << " sorted integers: ";
std::vector<int> l2;
for (size_t i = 0; i < n; ++i) {
std::cin >> x;
l2.push_back(x);
}
ListNode* h1 = create_list_from_vec(l1);
ListNode* h2 = create_list_from_vec(l2);
print_list(h1);
print_list(h2);
Solution solution;
ListNode* result = solution.mergeTwoLists(h1, h2);
print_list(result);
}
return 0;
}