45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
+ ** 方法一:递归**
49
+
50
+ ** 方法二:迭代**
51
+
48
52
迭代遍历两链表,比较节点值 val 的大小,进行节点串联,得到最终链表。
49
53
50
54
<!-- tabs:start -->
60
64
# self.val = val
61
65
# self.next = next
62
66
class Solution :
63
- def mergeTwoLists (self , l1 : ListNode, l2 : ListNode) -> ListNode:
67
+ def mergeTwoLists (self , list1 : Optional[ListNode], list2 : Optional[ListNode]) -> Optional[ListNode]:
68
+ if list1 is None or list2 is None :
69
+ return list1 or list2
70
+ if list1.val <= list2.val:
71
+ list1.next = self .mergeTwoLists(list1.next, list2)
72
+ return list1
73
+ else :
74
+ list2.next = self .mergeTwoLists(list1, list2.next)
75
+ return list2
76
+ ```
77
+
78
+ ``` python
79
+ # Definition for singly-linked list.
80
+ # class ListNode:
81
+ # def __init__(self, val=0, next=None):
82
+ # self.val = val
83
+ # self.next = next
84
+ class Solution :
85
+ def mergeTwoLists (self , list1 : Optional[ListNode], list2 : Optional[ListNode]) -> Optional[ListNode]:
64
86
dummy = ListNode()
65
- cur = dummy
66
- while l1 and l2 :
67
- if l1 .val <= l2 .val:
68
- cur .next = l1
69
- l1 = l1 .next
87
+ curr = dummy
88
+ while list1 and list2 :
89
+ if list1 .val <= list2 .val:
90
+ curr .next = list1
91
+ list1 = list1 .next
70
92
else :
71
- cur .next = l2
72
- l2 = l2 .next
73
- cur = cur .next
74
- cur .next = l1 or l2
93
+ curr .next = list2
94
+ list2 = list2 .next
95
+ curr = curr .next
96
+ curr .next = list1 or list2
75
97
return dummy.next
76
98
```
77
99
@@ -91,20 +113,50 @@ class Solution:
91
113
* }
92
114
*/
93
115
class Solution {
94
- public ListNode mergeTwoLists (ListNode l1 , ListNode l2 ) {
95
- ListNode dummy = new ListNode (0 );
96
- ListNode cur = dummy;
97
- while (l1 != null && l2 != null ) {
98
- if (l1. val <= l2. val) {
99
- cur. next = l1;
100
- l1 = l1. next;
116
+ public ListNode mergeTwoLists (ListNode list1 , ListNode list2 ) {
117
+ if (list1 == null ) {
118
+ return list2;
119
+ }
120
+ if (list2 == null ) {
121
+ return list1;
122
+ }
123
+ if (list1. val <= list2. val) {
124
+ list1. next = mergeTwoLists(list1. next, list2);
125
+ return list1;
126
+ } else {
127
+ list2. next = mergeTwoLists(list1, list2. next);
128
+ return list2;
129
+ }
130
+ }
131
+ }
132
+ ```
133
+
134
+ ``` java
135
+ /**
136
+ * Definition for singly-linked list.
137
+ * public class ListNode {
138
+ * int val;
139
+ * ListNode next;
140
+ * ListNode() {}
141
+ * ListNode(int val) { this.val = val; }
142
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
143
+ * }
144
+ */
145
+ class Solution {
146
+ public ListNode mergeTwoLists (ListNode list1 , ListNode list2 ) {
147
+ ListNode dummy = new ListNode ();
148
+ ListNode curr = dummy;
149
+ while (list1 != null && list2 != null ) {
150
+ if (list1. val <= list2. val) {
151
+ curr. next = list1;
152
+ list1 = list1. next;
101
153
} else {
102
- cur . next = l2 ;
103
- l2 = l2 . next;
154
+ curr . next = list2 ;
155
+ list2 = list2 . next;
104
156
}
105
- cur = cur . next;
157
+ curr = curr . next;
106
158
}
107
- cur . next = l1 == null ? l2 : l1 ;
159
+ curr . next = list1 == null ? list2 : list1 ;
108
160
return dummy. next;
109
161
}
110
162
}
@@ -125,20 +177,54 @@ class Solution {
125
177
*/
126
178
class Solution {
127
179
public:
128
- ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
180
+ ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
181
+ if (!list1) return list2;
182
+ if (!list2) return list1;
183
+ if (list1->val <= list2->val)
184
+ {
185
+ list1->next = mergeTwoLists(list1->next, list2);
186
+ return list1;
187
+ }
188
+ else
189
+ {
190
+ list2->next = mergeTwoLists(list1, list2->next);
191
+ return list2;
192
+ }
193
+ }
194
+ };
195
+ ```
196
+
197
+ ```cpp
198
+ /**
199
+ * Definition for singly-linked list.
200
+ * struct ListNode {
201
+ * int val;
202
+ * ListNode *next;
203
+ * ListNode() : val(0), next(nullptr) {}
204
+ * ListNode(int x) : val(x), next(nullptr) {}
205
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
206
+ * };
207
+ */
208
+ class Solution {
209
+ public:
210
+ ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
129
211
ListNode* dummy = new ListNode();
130
- ListNode* cur = dummy;
131
- while (l1 && l2) {
132
- if (l1->val <= l2->val) {
133
- cur->next = l1;
134
- l1 = l1->next;
135
- } else {
136
- cur->next = l2;
137
- l2 = l2->next;
212
+ ListNode* curr = dummy;
213
+ while (list1 && list2)
214
+ {
215
+ if (list1->val <= list2->val)
216
+ {
217
+ curr->next = list1;
218
+ list1 = list1->next;
219
+ }
220
+ else
221
+ {
222
+ curr->next = list2;
223
+ list2 = list2->next;
138
224
}
139
- cur = cur ->next;
225
+ curr = curr ->next;
140
226
}
141
- cur ->next = l1 ? l1 : l2 ;
227
+ curr ->next = list1 ? list1 : list2 ;
142
228
return dummy->next;
143
229
}
144
230
};
@@ -155,24 +241,51 @@ public:
155
241
* }
156
242
*/
157
243
/**
158
- * @param {ListNode} l1
159
- * @param {ListNode} l2
244
+ * @param {ListNode} list1
245
+ * @param {ListNode} list2
160
246
* @return {ListNode}
161
247
*/
162
- var mergeTwoLists = function (l1, l2) {
248
+ var mergeTwoLists = function (list1 , list2 ) {
249
+ if (! list1 || ! list2) {
250
+ return list1 || list2;
251
+ }
252
+ if (list1 .val <= list2 .val ) {
253
+ list1 .next = mergeTwoLists (list1 .next , list2);
254
+ return list1;
255
+ } else {
256
+ list2 .next = mergeTwoLists (list1, list2 .next );
257
+ return list2;
258
+ }
259
+ };
260
+ ```
261
+
262
+ ``` js
263
+ /**
264
+ * Definition for singly-linked list.
265
+ * function ListNode(val, next) {
266
+ * this.val = (val===undefined ? 0 : val)
267
+ * this.next = (next===undefined ? null : next)
268
+ * }
269
+ */
270
+ /**
271
+ * @param {ListNode} list1
272
+ * @param {ListNode} list2
273
+ * @return {ListNode}
274
+ */
275
+ var mergeTwoLists = function (list1 , list2 ) {
163
276
const dummy = new ListNode ();
164
- let cur = dummy;
165
- while (l1 && l2 ) {
166
- if (l1 .val <= l2 .val) {
167
- cur .next = l1 ;
168
- l1 = l1 .next;
277
+ let curr = dummy;
278
+ while (list1 && list2 ) {
279
+ if (list1 .val <= list2 .val ) {
280
+ curr .next = list1 ;
281
+ list1 = list1 .next ;
169
282
} else {
170
- cur .next = l2 ;
171
- l2 = l2 .next;
283
+ curr .next = list2 ;
284
+ list2 = list2 .next ;
172
285
}
173
- cur = cur .next;
286
+ curr = curr .next ;
174
287
}
175
- cur .next = l1 || l2 ;
288
+ curr .next = list1 || list2 ;
176
289
return dummy .next ;
177
290
};
178
291
```
@@ -187,25 +300,50 @@ var mergeTwoLists = function (l1, l2) {
187
300
* Next *ListNode
188
301
* }
189
302
*/
190
- func mergeTwoLists (l1 *ListNode , l2 *ListNode ) *ListNode {
191
- dummy := &ListNode{}
192
- cur := dummy
193
- for l1 != nil && l2 != nil {
194
- if l1.Val <= l2.Val {
195
- cur.Next = l1
196
- l1 = l1.Next
197
- } else {
198
- cur.Next = l2
199
- l2 = l2.Next
200
- }
201
- cur = cur.Next
202
- }
203
- if l1 != nil {
204
- cur.Next = l1
205
- } else if l2 != nil {
206
- cur.Next = l2
207
- }
208
- return dummy.Next
303
+ func mergeTwoLists (list1 *ListNode , list2 *ListNode ) *ListNode {
304
+ if list1 == nil {
305
+ return list2
306
+ }
307
+ if list2 == nil {
308
+ return list1
309
+ }
310
+ if list1.Val <= list2.Val {
311
+ list1.Next = mergeTwoLists (list1.Next , list2)
312
+ return list1
313
+ } else {
314
+ list2.Next = mergeTwoLists (list1, list2.Next )
315
+ return list2
316
+ }
317
+ }
318
+ ```
319
+
320
+ ``` go
321
+ /* *
322
+ * Definition for singly-linked list.
323
+ * type ListNode struct {
324
+ * Val int
325
+ * Next *ListNode
326
+ * }
327
+ */
328
+ func mergeTwoLists (list1 *ListNode , list2 *ListNode ) *ListNode {
329
+ dummy := &ListNode{}
330
+ curr := dummy
331
+ for list1 != nil && list2 != nil {
332
+ if list1.Val <= list2.Val {
333
+ curr.Next = list1
334
+ list1 = list1.Next
335
+ } else {
336
+ curr.Next = list2
337
+ list2 = list2.Next
338
+ }
339
+ curr = curr.Next
340
+ }
341
+ if list1 != nil {
342
+ curr.Next = list1
343
+ } else {
344
+ curr.Next = list2
345
+ }
346
+ return dummy.Next
209
347
}
210
348
```
211
349
@@ -220,23 +358,23 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
220
358
# @next = _next
221
359
# end
222
360
# end
223
- # @ param {ListNode} l1
224
- # @ param {ListNode} l2
361
+ # @ param {ListNode} list1
362
+ # @ param {ListNode} list2
225
363
# @ return {ListNode}
226
- def merge_two_lists (l1 , l2 )
364
+ def merge_two_lists (list1 , list2 )
227
365
dummy = ListNode .new ()
228
366
cur = dummy
229
- while l1 && l2
230
- if l1 .val <= l2 .val
231
- cur.next = l1
232
- l1 = l1 .next
367
+ while list1 && list2
368
+ if list1 .val <= list2 .val
369
+ cur.next = list1
370
+ list1 = list1 .next
233
371
else
234
- cur.next = l2
235
- l2 = l2 .next
372
+ cur.next = list2
373
+ list2 = list2 .next
236
374
end
237
375
cur = cur.next
238
376
end
239
- cur.next = l1 || l2
377
+ cur.next = list1 || list2
240
378
dummy.next
241
379
end
242
380
```
@@ -256,20 +394,24 @@ end
256
394
* }
257
395
*/
258
396
public class Solution {
259
- public ListNode MergeTwoLists (ListNode l1 , ListNode l2 ) {
397
+ public ListNode MergeTwoLists (ListNode list1 , ListNode list2 ) {
260
398
ListNode dummy = new ListNode ();
261
399
ListNode cur = dummy ;
262
- while (l1 != null && l2 != null ) {
263
- if (l1 .val <= l2 .val ) {
264
- cur .next = l1 ;
265
- l1 = l1 .next ;
266
- } else {
267
- cur .next = l2 ;
268
- l2 = l2 .next ;
400
+ while (list1 != null && list2 != null )
401
+ {
402
+ if (list1 .val <= list2 .val )
403
+ {
404
+ cur .next = list1 ;
405
+ list1 = list1 .next ;
406
+ }
407
+ else
408
+ {
409
+ cur .next = list2 ;
410
+ list2 = list2 .next ;
269
411
}
270
412
cur = cur .next ;
271
413
}
272
- cur .next = l1 == null ? l2 : l1 ;
414
+ cur .next = list1 == null ? list2 : list1 ;
273
415
return dummy .next ;
274
416
}
275
417
}
0 commit comments