Skip to content

Commit 0917b81

Browse files
committed
Add solution 023
1 parent 5d70af4 commit 0917b81

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Complete solutions to Leetcode problems, updated daily.
3030

3131
| # | Title | Tags |
3232
|---|---|---|
33+
| 023 | [Merge k Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/023.Merge%20k%20Sorted%20Lists) | `Linked List`, `Divide and Conquer`, `Heap` |
3334

3435
## Contributions
3536
I'm looking for long-term contributors/partners to this repo! Send me PRs if you're interested! See the following:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## 合并K个排序链表
2+
### 题目描述
3+
4+
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
5+
6+
示例:
7+
```
8+
输入:
9+
[
10+
1->4->5,
11+
1->3->4,
12+
2->6
13+
]
14+
输出: 1->1->2->3->4->4->5->6
15+
```
16+
17+
### 解法
18+
从链表数组索引 0 开始,[合并前后相邻两个有序链表](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists),放在后一个链表位置上,依次循环下去...最后 lists[len - 1] 即为合并后的链表。注意处理链表数组元素小于 2 的情况。
19+
20+
```java
21+
/**
22+
* Definition for singly-linked list.
23+
* public class ListNode {
24+
* int val;
25+
* ListNode next;
26+
* ListNode(int x) { val = x; }
27+
* }
28+
*/
29+
class Solution {
30+
public ListNode mergeKLists(ListNode[] lists) {
31+
if (lists == null || lists.length == 0) {
32+
return null;
33+
}
34+
35+
int len = lists.length;
36+
if (len == 1) {
37+
return lists[0];
38+
}
39+
40+
// 合并前后两个链表,结果放在后一个链表位置上,依次循环下去
41+
for (int i = 0; i < len - 1; ++i) {
42+
lists[i + 1] = mergeTwoLists(lists[i], lists[i + 1]);
43+
}
44+
return lists[len - 1];
45+
46+
}
47+
48+
/**
49+
* 合并两个有序链表
50+
* @param l1
51+
* @param l2
52+
* @return listNode
53+
*/
54+
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
55+
if (l1 == null) {
56+
return l2;
57+
}
58+
if (l2 == null) {
59+
return l1;
60+
}
61+
if (l1.val < l2.val) {
62+
l1.next = mergeTwoLists(l1.next, l2);
63+
return l1;
64+
}
65+
l2.next = mergeTwoLists(l1, l2.next);
66+
return l2;
67+
}
68+
}
69+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public ListNode mergeKLists(ListNode[] lists) {
3+
if (lists == null || lists.length == 0) {
4+
return null;
5+
}
6+
7+
int len = lists.length;
8+
if (len == 1) {
9+
return lists[0];
10+
}
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+
18+
}
19+
20+
/**
21+
* 合并两个有序链表
22+
* @param l1
23+
* @param l2
24+
* @return listNode
25+
*/
26+
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;
36+
}
37+
l2.next = mergeTwoLists(l1, l2.next);
38+
return l2;
39+
}
40+
}

solution/023.Merge k Sorted Lists/Solution.py

Whitespace-only changes.

0 commit comments

Comments
 (0)