Skip to content

Commit ddc1e51

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0021
1 parent bf2c587 commit ddc1e51

File tree

9 files changed

+160
-101
lines changed

9 files changed

+160
-101
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
1. [从尾到头打印链表](/lcof/面试题06.%20从尾到头打印链表/README.md)
5959
1. [删除链表的节点](/lcof/面试题18.%20删除链表的节点/README.md)
6060
1. [链表中倒数第 k 个节点](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README.md)
61+
1. [合并两个有序链表](/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/README.md)
6162
1. [反转链表](/solution/0200-0299/0206.Reverse%20Linked%20List/README.md)
6263
1. [回文链表](/solution/0200-0299/0234.Palindrome%20Linked%20List/README.md)
6364
1. [环形链表](/solution/0100-0199/0141.Linked%20List%20Cycle/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
5656

5757
1. [Delete Node in a Linked List](/solution/0200-0299/0237.Delete%20Node%20in%20a%20Linked%20List/README_EN.md)
5858
1. [Kth Node From End of List](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README_EN.md)
59+
1. [Merge Two Sorted Lists](/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/README_EN.md)
5960
1. [Reverse Linked List](/solution/0200-0299/0206.Reverse%20Linked%20List/README_EN.md)
6061
1. [Palindrome Linked List](/solution/0200-0299/0234.Palindrome%20Linked%20List/README_EN.md)
6162
1. [Linked List Cycle](/solution/0100-0199/0141.Linked%20List%20Cycle/README_EN.md)

lcof/面试题25. 合并两个排序的链表/README.md

+20-36
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,18 @@
3232

3333
class Solution:
3434
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
35-
if l1 is None or l2 is None:
36-
return l1 or l2
37-
head = ListNode(0)
38-
p = head
35+
dummy = ListNode()
36+
cur = dummy
3937
while l1 and l2:
40-
if l1.val < l2.val:
41-
t = l1.next
42-
p.next = l1
43-
p = l1
44-
l1 = t
38+
if l1.val <= l2.val:
39+
cur.next = l1
40+
l1 = l1.next
4541
else:
46-
t = l2.next
47-
p.next = l2
48-
p = l2
49-
l2 = t
50-
51-
p.next = l1 or l2
52-
return head.next
42+
cur.next = l2
43+
l2 = l2.next
44+
cur = cur.next
45+
cur.next = l1 or l2
46+
return dummy.next
5347
```
5448

5549
### **Java**
@@ -65,30 +59,20 @@ class Solution:
6559
*/
6660
class Solution {
6761
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
68-
if (l1 == null) {
69-
return l2;
70-
}
71-
if (l2 == null) {
72-
return l1;
73-
}
74-
75-
ListNode head = new ListNode(0);
76-
ListNode p = head;
62+
ListNode dummy = new ListNode(0);
63+
ListNode cur = dummy;
7764
while (l1 != null && l2 != null) {
78-
if (l1.val < l2.val) {
79-
ListNode t = l1.next;
80-
p.next = l1;
81-
p = l1;
82-
l1 = t;
65+
if (l1.val <= l2.val) {
66+
cur.next = l1;
67+
l1 = l1.next;
8368
} else {
84-
ListNode t = l2.next;
85-
p.next = l2;
86-
p = l2;
87-
l2 = t;
69+
cur.next = l2;
70+
l2 = l2.next;
8871
}
72+
cur = cur.next;
8973
}
90-
p.next = l1 == null ? l2 : l1;
91-
return head.next;
74+
cur.next = l1 == null ? l2 : l1;
75+
return dummy.next;
9276
}
9377
}
9478
```

lcof/面试题25. 合并两个排序的链表/Solution.java

+10-20
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,19 @@
88
*/
99
class Solution {
1010
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11-
if (l1 == null) {
12-
return l2;
13-
}
14-
if (l2 == null) {
15-
return l1;
16-
}
17-
18-
ListNode head = new ListNode(0);
19-
ListNode p = head;
11+
ListNode dummy = new ListNode(0);
12+
ListNode cur = dummy;
2013
while (l1 != null && l2 != null) {
21-
if (l1.val < l2.val) {
22-
ListNode t = l1.next;
23-
p.next = l1;
24-
p = l1;
25-
l1 = t;
14+
if (l1.val <= l2.val) {
15+
cur.next = l1;
16+
l1 = l1.next;
2617
} else {
27-
ListNode t = l2.next;
28-
p.next = l2;
29-
p = l2;
30-
l2 = t;
18+
cur.next = l2;
19+
l2 = l2.next;
3120
}
21+
cur = cur.next;
3222
}
33-
p.next = l1 == null ? l2 : l1;
34-
return head.next;
23+
cur.next = l1 == null ? l2 : l1;
24+
return dummy.next;
3525
}
3626
}

lcof/面试题25. 合并两个排序的链表/Solution.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@
66

77
class Solution:
88
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
9-
if l1 is None or l2 is None:
10-
return l1 or l2
11-
head = ListNode(0)
12-
p = head
9+
dummy = ListNode()
10+
cur = dummy
1311
while l1 and l2:
14-
if l1.val < l2.val:
15-
t = l1.next
16-
p.next = l1
17-
p = l1
18-
l1 = t
12+
if l1.val <= l2.val:
13+
cur.next = l1
14+
l1 = l1.next
1915
else:
20-
t = l2.next
21-
p.next = l2
22-
p = l2
23-
l2 = t
24-
25-
p.next = l1 or l2
26-
return head.next
16+
cur.next = l2
17+
l2 = l2.next
18+
cur = cur.next
19+
cur.next = l1 or l2
20+
return dummy.next

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

+47-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,60 @@
2424
<!-- 这里可写当前语言的特殊实现逻辑 -->
2525

2626
```python
27-
27+
# Definition for singly-linked list.
28+
# class ListNode:
29+
# def __init__(self, val=0, next=None):
30+
# self.val = val
31+
# self.next = next
32+
class Solution:
33+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
34+
dummy = ListNode()
35+
cur = dummy
36+
while l1 and l2:
37+
if l1.val <= l2.val:
38+
cur.next = l1
39+
l1 = l1.next
40+
else:
41+
cur.next = l2
42+
l2 = l2.next
43+
cur = cur.next
44+
cur.next = l1 or l2
45+
return dummy.next
2846
```
2947

3048
### **Java**
3149

3250
<!-- 这里可写当前语言的特殊实现逻辑 -->
3351

3452
```java
35-
53+
/**
54+
* Definition for singly-linked list.
55+
* public class ListNode {
56+
* int val;
57+
* ListNode next;
58+
* ListNode() {}
59+
* ListNode(int val) { this.val = val; }
60+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
61+
* }
62+
*/
63+
class Solution {
64+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
65+
ListNode dummy = new ListNode(0);
66+
ListNode cur = dummy;
67+
while (l1 != null && l2 != null) {
68+
if (l1.val <= l2.val) {
69+
cur.next = l1;
70+
l1 = l1.next;
71+
} else {
72+
cur.next = l2;
73+
l2 = l2.next;
74+
}
75+
cur = cur.next;
76+
}
77+
cur.next = l1 == null ? l2 : l1;
78+
return dummy.next;
79+
}
80+
}
3681
```
3782

3883
### **...**

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

+47-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,58 @@
2525
### **Python3**
2626

2727
```python
28-
28+
# Definition for singly-linked list.
29+
# class ListNode:
30+
# def __init__(self, val=0, next=None):
31+
# self.val = val
32+
# self.next = next
33+
class Solution:
34+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
35+
dummy = ListNode()
36+
cur = dummy
37+
while l1 and l2:
38+
if l1.val <= l2.val:
39+
cur.next = l1
40+
l1 = l1.next
41+
else:
42+
cur.next = l2
43+
l2 = l2.next
44+
cur = cur.next
45+
cur.next = l1 or l2
46+
return dummy.next
2947
```
3048

3149
### **Java**
3250

3351
```java
34-
52+
/**
53+
* Definition for singly-linked list.
54+
* public class ListNode {
55+
* int val;
56+
* ListNode next;
57+
* ListNode() {}
58+
* ListNode(int val) { this.val = val; }
59+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
60+
* }
61+
*/
62+
class Solution {
63+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
64+
ListNode dummy = new ListNode(0);
65+
ListNode cur = dummy;
66+
while (l1 != null && l2 != null) {
67+
if (l1.val <= l2.val) {
68+
cur.next = l1;
69+
l1 = l1.next;
70+
} else {
71+
cur.next = l2;
72+
l2 = l2.next;
73+
}
74+
cur = cur.next;
75+
}
76+
cur.next = l1 == null ? l2 : l1;
77+
return dummy.next;
78+
}
79+
}
3580
```
3681

3782
### **...**

solution/0000-0099/0021.Merge Two Sorted Lists/Solution.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
1012
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11-
ListNode head = new ListNode(0);
12-
head.next = null;
13-
ListNode current = head;
13+
ListNode dummy = new ListNode(0);
14+
ListNode cur = dummy;
1415
while (l1 != null && l2 != null) {
1516
if (l1.val <= l2.val) {
16-
current.next = l1;
17+
cur.next = l1;
1718
l1 = l1.next;
1819
} else {
19-
current.next = l2;
20+
cur.next = l2;
2021
l2 = l2.next;
2122
}
22-
current = current.next;
23+
cur = cur.next;
2324
}
24-
current.next = l1 != null ? l1 : l2;
25-
return head.next;
25+
cur.next = l1 == null ? l2 : l1;
26+
return dummy.next;
2627
}
2728
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
76
class Solution:
8-
def mergeTwoLists(self, l1, l2):
9-
"""
10-
:type l1: ListNode
11-
:type l2: ListNode
12-
:rtype: ListNode
13-
"""
7+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
8+
dummy = ListNode()
9+
cur = dummy
1410
while l1 and l2:
15-
if l1.val < l2.val:
16-
l1.next=self.mergeTwoLists(l1.next,l2)
17-
return l1
11+
if l1.val <= l2.val:
12+
cur.next = l1
13+
l1 = l1.next
1814
else:
19-
l2.next=self.mergeTwoLists(l1,l2.next)
20-
return l2
21-
return l1 or l2
15+
cur.next = l2
16+
l2 = l2.next
17+
cur = cur.next
18+
cur.next = l1 or l2
19+
return dummy.next

0 commit comments

Comments
 (0)