Skip to content

Commit a35b68a

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

File tree

6 files changed

+192
-58
lines changed

6 files changed

+192
-58
lines changed

README.md

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

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
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)
5959
1. [Merge Two Sorted Lists](/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/README_EN.md)
60+
1. [Merge k Sorted Lists](/solution/0000-0099/0023.Merge%20k%20Sorted%20Lists/README_EN.md)
6061
1. [Reverse Linked List](/solution/0200-0299/0206.Reverse%20Linked%20List/README_EN.md)
6162
1. [Palindrome Linked List](/solution/0200-0299/0234.Palindrome%20Linked%20List/README_EN.md)
6263
1. [Linked List Cycle](/solution/0100-0199/0141.Linked%20List%20Cycle/README_EN.md)

solution/0000-0099/0023.Merge k Sorted Lists/README.md

+69-2
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,89 @@
2121

2222
<!-- 这里可写通用的实现逻辑 -->
2323

24+
合并前后两个链表,结果放在后一个链表位置上,依次循环下去。
25+
2426
<!-- tabs:start -->
2527

2628
### **Python3**
2729

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

3032
```python
31-
33+
# Definition for singly-linked list.
34+
# class ListNode:
35+
# def __init__(self, val=0, next=None):
36+
# self.val = val
37+
# self.next = next
38+
class Solution:
39+
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
40+
if not lists:
41+
return None
42+
n = len(lists)
43+
for i in range(1, n):
44+
lists[i] = self.mergeTwoLists(lists[i - 1], lists[i])
45+
return lists[n - 1]
46+
47+
48+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
49+
dummy = ListNode()
50+
cur = dummy
51+
while l1 and l2:
52+
if l1.val <= l2.val:
53+
cur.next = l1
54+
l1 = l1.next
55+
else:
56+
cur.next = l2
57+
l2 = l2.next
58+
cur = cur.next
59+
cur.next = l1 or l2
60+
return dummy.next
3261
```
3362

3463
### **Java**
3564

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

3867
```java
39-
68+
/**
69+
* Definition for singly-linked list.
70+
* public class ListNode {
71+
* int val;
72+
* ListNode next;
73+
* ListNode() {}
74+
* ListNode(int val) { this.val = val; }
75+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
76+
* }
77+
*/
78+
class Solution {
79+
public ListNode mergeKLists(ListNode[] lists) {
80+
int n;
81+
if (lists == null || (n = lists.length) == 0) {
82+
return null;
83+
}
84+
for (int i = 1; i < n; ++i) {
85+
lists[i] = mergeTwoLists(lists[i - 1], lists[i]);
86+
}
87+
return lists[n - 1];
88+
}
89+
90+
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
91+
ListNode dummy = new ListNode(0);
92+
ListNode cur = dummy;
93+
while (l1 != null && l2 != null) {
94+
if (l1.val <= l2.val) {
95+
cur.next = l1;
96+
l1 = l1.next;
97+
} else {
98+
cur.next = l2;
99+
l2 = l2.next;
100+
}
101+
cur = cur.next;
102+
}
103+
cur.next = l1 == null ? l2 : l1;
104+
return dummy.next;
105+
}
106+
}
40107
```
41108

42109
### **...**

solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md

+67-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,78 @@
3333
### **Python3**
3434

3535
```python
36-
36+
# Definition for singly-linked list.
37+
# class ListNode:
38+
# def __init__(self, val=0, next=None):
39+
# self.val = val
40+
# self.next = next
41+
class Solution:
42+
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
43+
if not lists:
44+
return None
45+
n = len(lists)
46+
for i in range(1, n):
47+
lists[i] = self.mergeTwoLists(lists[i - 1], lists[i])
48+
return lists[n - 1]
49+
50+
51+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
52+
dummy = ListNode()
53+
cur = dummy
54+
while l1 and l2:
55+
if l1.val <= l2.val:
56+
cur.next = l1
57+
l1 = l1.next
58+
else:
59+
cur.next = l2
60+
l2 = l2.next
61+
cur = cur.next
62+
cur.next = l1 or l2
63+
return dummy.next
3764
```
3865

3966
### **Java**
4067

4168
```java
42-
69+
/**
70+
* Definition for singly-linked list.
71+
* public class ListNode {
72+
* int val;
73+
* ListNode next;
74+
* ListNode() {}
75+
* ListNode(int val) { this.val = val; }
76+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
77+
* }
78+
*/
79+
class Solution {
80+
public ListNode mergeKLists(ListNode[] lists) {
81+
int n;
82+
if (lists == null || (n = lists.length) == 0) {
83+
return null;
84+
}
85+
for (int i = 1; i < n; ++i) {
86+
lists[i] = mergeTwoLists(lists[i - 1], lists[i]);
87+
}
88+
return lists[n - 1];
89+
}
90+
91+
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
92+
ListNode dummy = new ListNode(0);
93+
ListNode cur = dummy;
94+
while (l1 != null && l2 != null) {
95+
if (l1.val <= l2.val) {
96+
cur.next = l1;
97+
l1 = l1.next;
98+
} else {
99+
cur.next = l2;
100+
l2 = l2.next;
101+
}
102+
cur = cur.next;
103+
}
104+
cur.next = l1 == null ? l2 : l1;
105+
return dummy.next;
106+
}
107+
}
43108
```
44109

45110
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
111
class Solution {
212
public ListNode mergeKLists(ListNode[] lists) {
3-
if (lists == null || lists.length == 0) {
13+
int n;
14+
if (lists == null || (n = lists.length) == 0) {
415
return null;
516
}
6-
7-
int len = lists.length;
8-
if (len == 1) {
9-
return lists[0];
17+
for (int i = 1; i < n; ++i) {
18+
lists[i] = mergeTwoLists(lists[i - 1], lists[i]);
1019
}
11-
12-
// 合并前后两个链表,结果放在后一个链表位置上,依次循环下去
13-
for (int i = 0; i < len - 1; ++i) {
14-
lists[i + 1] = mergeTwoLists(lists[i], lists[i + 1]);
15-
}
16-
return lists[len - 1];
17-
20+
return lists[n - 1];
1821
}
19-
20-
/**
21-
* 合并两个有序链表
22-
* @param l1
23-
* @param l2
24-
* @return listNode
25-
*/
22+
2623
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
27-
if (l1 == null) {
28-
return l2;
29-
}
30-
if (l2 == null) {
31-
return l1;
32-
}
33-
if (l1.val < l2.val) {
34-
l1.next = mergeTwoLists(l1.next, l2);
35-
return l1;
24+
ListNode dummy = new ListNode(0);
25+
ListNode cur = dummy;
26+
while (l1 != null && l2 != null) {
27+
if (l1.val <= l2.val) {
28+
cur.next = l1;
29+
l1 = l1.next;
30+
} else {
31+
cur.next = l2;
32+
l2 = l2.next;
33+
}
34+
cur = cur.next;
3635
}
37-
l2.next = mergeTwoLists(l1, l2.next);
38-
return l2;
36+
cur.next = l1 == null ? l2 : l1;
37+
return dummy.next;
3938
}
4039
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
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 mergeKLists(self, lists):
9-
"""
10-
:type lists: List[ListNode]
11-
:rtype: ListNode
12-
"""
13-
list0=[]
14-
for i in lists:
15-
while i:
16-
list0.append(i.val)
17-
i=i.next
18-
if list0==[]:
19-
return []
20-
list0.sort(reverse=True)
21-
ln=ListNode(0)
22-
tmp=ln
23-
while list0:
24-
tmp1=list0.pop()
25-
ln.next=ListNode(tmp1)
26-
ln=ln.next
27-
return tmp.next
7+
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
8+
if not lists:
9+
return None
10+
n = len(lists)
11+
for i in range(1, n):
12+
lists[i] = self.mergeTwoLists(lists[i - 1], lists[i])
13+
return lists[n - 1]
14+
15+
16+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
17+
dummy = ListNode()
18+
cur = dummy
19+
while l1 and l2:
20+
if l1.val <= l2.val:
21+
cur.next = l1
22+
l1 = l1.next
23+
else:
24+
cur.next = l2
25+
l2 = l2.next
26+
cur = cur.next
27+
cur.next = l1 or l2
28+
return dummy.next

0 commit comments

Comments
 (0)