Skip to content

Commit 1f00bcb

Browse files
committed
feat: add solutions to lc problem: No.0021
No.0021.Merge Two Sorted Lists
1 parent 7c7fdd2 commit 1f00bcb

File tree

11 files changed

+546
-255
lines changed

11 files changed

+546
-255
lines changed

solution/0000-0099/0021.Merge Two Sorted Lists/README.md

Lines changed: 224 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:递归**
49+
50+
**方法二:迭代**
51+
4852
迭代遍历两链表,比较节点值 val 的大小,进行节点串联,得到最终链表。
4953

5054
<!-- tabs:start -->
@@ -60,18 +64,36 @@
6064
# self.val = val
6165
# self.next = next
6266
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]:
6486
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
7092
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
7597
return dummy.next
7698
```
7799

@@ -91,20 +113,50 @@ class Solution:
91113
* }
92114
*/
93115
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;
101153
} else {
102-
cur.next = l2;
103-
l2 = l2.next;
154+
curr.next = list2;
155+
list2 = list2.next;
104156
}
105-
cur = cur.next;
157+
curr = curr.next;
106158
}
107-
cur.next = l1 == null ? l2 : l1;
159+
curr.next = list1 == null ? list2 : list1;
108160
return dummy.next;
109161
}
110162
}
@@ -125,20 +177,54 @@ class Solution {
125177
*/
126178
class Solution {
127179
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) {
129211
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;
138224
}
139-
cur = cur->next;
225+
curr = curr->next;
140226
}
141-
cur->next = l1 ? l1 : l2;
227+
curr->next = list1 ? list1 : list2;
142228
return dummy->next;
143229
}
144230
};
@@ -155,24 +241,51 @@ public:
155241
* }
156242
*/
157243
/**
158-
* @param {ListNode} l1
159-
* @param {ListNode} l2
244+
* @param {ListNode} list1
245+
* @param {ListNode} list2
160246
* @return {ListNode}
161247
*/
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) {
163276
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;
169282
} else {
170-
cur.next = l2;
171-
l2 = l2.next;
283+
curr.next = list2;
284+
list2 = list2.next;
172285
}
173-
cur = cur.next;
286+
curr = curr.next;
174287
}
175-
cur.next = l1 || l2;
288+
curr.next = list1 || list2;
176289
return dummy.next;
177290
};
178291
```
@@ -187,25 +300,50 @@ var mergeTwoLists = function (l1, l2) {
187300
* Next *ListNode
188301
* }
189302
*/
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
209347
}
210348
```
211349

@@ -220,23 +358,23 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
220358
# @next = _next
221359
# end
222360
# end
223-
# @param {ListNode} l1
224-
# @param {ListNode} l2
361+
# @param {ListNode} list1
362+
# @param {ListNode} list2
225363
# @return {ListNode}
226-
def merge_two_lists(l1, l2)
364+
def merge_two_lists(list1, list2)
227365
dummy = ListNode.new()
228366
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
233371
else
234-
cur.next = l2
235-
l2 = l2.next
372+
cur.next = list2
373+
list2 = list2.next
236374
end
237375
cur = cur.next
238376
end
239-
cur.next = l1 || l2
377+
cur.next = list1 || list2
240378
dummy.next
241379
end
242380
```
@@ -256,20 +394,24 @@ end
256394
* }
257395
*/
258396
public class Solution {
259-
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
397+
public ListNode MergeTwoLists(ListNode list1, ListNode list2) {
260398
ListNode dummy = new ListNode();
261399
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;
269411
}
270412
cur = cur.next;
271413
}
272-
cur.next = l1 == null ? l2 : l1;
414+
cur.next = list1 == null ? list2 : list1;
273415
return dummy.next;
274416
}
275417
}

0 commit comments

Comments
 (0)