-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path21.cpp
174 lines (168 loc) · 4.34 KB
/
21.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
__________________________________________________________________________________________________
8ms
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* cur = dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
cur = cur->next = l1;
l1 = l1->next;
} else {
cur = cur->next = l2;
l2 = l2->next;
}
}
if (l1) cur->next = l1;
if (l2) cur->next = l2;
return dummy->next;
}
};
__________________________________________________________________________________________________
12ms
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1 && !l2) return NULL;
ListNode *ans=NULL, *p;
if (!l1 || (l2 && l2->val <= l1->val)) {
ans = l2;
l2 = l2->next;
} else {
ans = l1;
l1 = l1->next;
}
p = ans;
while(l1||l2) {
if (!l1 || (l2 && l2->val <= l1->val)) {
p->next = l2;
l2 = l2->next;
} else {
p->next = l1;
l1 = l1->next;
}
p = p->next;
}
return ans;
}
};
__________________________________________________________________________________________________
16ms
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode(-1), *cur = dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 ? l1 : l2;
return dummy->next;
}
};
__________________________________________________________________________________________________
8908 kb
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
Solution(){
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == NULL){
return l2;
} else if (l2 == NULL){
return l1;
}
ListNode* l;
ListNode* cur = l;
if (l1->val <= l2->val){
l = l1;
cur = l1;
l1 = l1->next;
} else{
l = l2;
cur = l2;
l2 = l2->next;
}
while (l1 != NULL && l2 != NULL){
if (l1->val <= l2->val){
cur->next = l1;
l1 = l1->next;
} else{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if (l1 != NULL){
cur->next = l1;
}
if (l2 != NULL){
cur->next = l2;
}
return l;
}
};
__________________________________________________________________________________________________
8952 kb
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *head = new ListNode(0),*tmp = head;
while(true) {
while(l1 != NULL && l2 != NULL &&l1->val <= l2->val) {
tmp->next =l1;
l1 = l1->next;
tmp = tmp->next;
}
if(l1 == NULL) {
tmp ->next =l2;
break;
}
while(l2 != NULL && l1!= NULL &&l2->val <= l1->val) {
tmp->next = l2;
l2 = l2->next;
tmp = tmp->next;
}
if(l2 == NULL) {
tmp->next = l1;
break;
}
}
tmp = head;
head = head->next;
delete tmp;
return head;
}
};
__________________________________________________________________________________________________