Skip to content

Commit f9dd130

Browse files
committed
feat: add 023
1 parent 66e6d7b commit f9dd130

File tree

4 files changed

+180
-2
lines changed

4 files changed

+180
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
|:------------- |:------------- |:------------- |
7373
|4|[Median of Two Sorted Arrays][004]|Array, Binary Search, Divide and Conquer|
7474
|10|[Regular Expression Matching][010]|String, Dynamic Programmin, Backtracking|
75+
|23|[Merge k Sorted Lists][023]|Linked List, Divide and Conquer, Heap|
7576

7677

7778

@@ -124,3 +125,4 @@
124125

125126
[004]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/004/README.md
126127
[010]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/010/README.md
128+
[023]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/023/README.md

note/023/README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [Merge k Sorted Lists][title]
2+
3+
## Description
4+
5+
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
6+
7+
**Tags:** Linked List, Divide and Conquer, Heap
8+
9+
10+
## 思路0
11+
12+
题意是合并多个已排序的链表,分析并描述其复杂度,我们可以用分治法来两两合并他们,假设`k`为总链表个数,`N`为总元素个数,那么其时间复杂度为`O(Nlogk)`
13+
14+
``` java
15+
/**
16+
* Definition for singly-linked list.
17+
* public class ListNode {
18+
* int val;
19+
* ListNode next;
20+
* ListNode(int x) { val = x; }
21+
* }
22+
*/
23+
class Solution {
24+
public ListNode mergeKLists(ListNode[] lists) {
25+
if (lists.length == 0) return null;
26+
return helper(lists, 0, lists.length - 1);
27+
}
28+
29+
private ListNode helper(ListNode[] lists, int left, int right) {
30+
if (left >= right) return lists[left];
31+
int mid = left + right >>> 1;
32+
ListNode l0 = helper(lists, left, mid);
33+
ListNode l1 = helper(lists, mid + 1, right);
34+
return merge2Lists(l0, l1);
35+
}
36+
37+
private ListNode merge2Lists(ListNode l0, ListNode l1) {
38+
ListNode node = new ListNode(0), tmp = node;
39+
while (l0 != null && l1 != null) {
40+
if (l0.val <= l1.val) {
41+
tmp.next = new ListNode(l0.val);
42+
l0 = l0.next;
43+
} else {
44+
tmp.next = new ListNode(l1.val);
45+
l1 = l1.next;
46+
}
47+
tmp = tmp.next;
48+
}
49+
tmp.next = l0 != null ? l0 : l1;
50+
return node.next;
51+
}
52+
}
53+
```
54+
55+
## 思路1
56+
57+
还有另一种思路是利用优先队列,该数据结构用到的是堆排序,所以对总链表个数为`k`的复杂度为`logk`,总元素为个数为`N`的话,其时间复杂度也为`O(Nlogk)`
58+
59+
``` java
60+
/**
61+
* Definition for singly-linked list.
62+
* public class ListNode {
63+
* int val;
64+
* ListNode next;
65+
* ListNode(int x) { val = x; }
66+
* }
67+
*/
68+
class Solution {
69+
public ListNode mergeKLists(ListNode[] lists) {
70+
if (lists.length == 0) return null;
71+
PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() {
72+
@Override
73+
public int compare(ListNode o1, ListNode o2) {
74+
if (o1.val < o2.val) return -1;
75+
else if (o1.val == o2.val) return 0;
76+
else return 1;
77+
}
78+
});
79+
ListNode node = new ListNode(0), tmp = node;
80+
for (ListNode l : lists) {
81+
if (l != null) queue.add(l);
82+
}
83+
while (!queue.isEmpty()) {
84+
tmp.next = queue.poll();
85+
tmp = tmp.next;
86+
if (tmp.next != null) queue.add(tmp.next);
87+
}
88+
return node.next;
89+
}
90+
}
91+
```
92+
93+
94+
## 结语
95+
96+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
97+
98+
99+
100+
[title]: https://leetcode.com/problems/merge-k-sorted-lists
101+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

src/com/blankj/easy/_010/Solution.java renamed to src/com/blankj/hard/_010/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package com.blankj.easy._010;
1+
package com.blankj.hard._010;
22

33
/**
44
* <pre>
55
* author: Blankj
66
* blog : http://blankj.com
7-
* time : 2017/04/24
7+
* time : 2017/10/13
88
* desc :
99
* </pre>
1010
*/
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.blankj.hard._023;
2+
3+
import com.blankj.structure.ListNode;
4+
5+
import java.util.Comparator;
6+
import java.util.PriorityQueue;
7+
8+
/**
9+
* <pre>
10+
* author: Blankj
11+
* blog : http://blankj.com
12+
* time : 2017/10/15
13+
* desc :
14+
* </pre>
15+
*/
16+
public class Solution {
17+
// public ListNode mergeKLists(ListNode[] lists) {
18+
// if (lists.length == 0) return null;
19+
// return helper(lists, 0, lists.length - 1);
20+
// }
21+
//
22+
// private ListNode helper(ListNode[] lists, int left, int right) {
23+
// if (left >= right) return lists[left];
24+
// int mid = left + right >>> 1;
25+
// ListNode l0 = helper(lists, left, mid);
26+
// ListNode l1 = helper(lists, mid + 1, right);
27+
// return merge2Lists(l0, l1);
28+
// }
29+
//
30+
// private ListNode merge2Lists(ListNode l0, ListNode l1) {
31+
// ListNode node = new ListNode(0), tmp = node;
32+
// while (l0 != null && l1 != null) {
33+
// if (l0.val <= l1.val) {
34+
// tmp.next = new ListNode(l0.val);
35+
// l0 = l0.next;
36+
// } else {
37+
// tmp.next = new ListNode(l1.val);
38+
// l1 = l1.next;
39+
// }
40+
// tmp = tmp.next;
41+
// }
42+
// tmp.next = l0 != null ? l0 : l1;
43+
// return node.next;
44+
// }
45+
46+
public ListNode mergeKLists(ListNode[] lists) {
47+
if (lists.length == 0) return null;
48+
PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() {
49+
@Override
50+
public int compare(ListNode o1, ListNode o2) {
51+
if (o1.val < o2.val) return -1;
52+
else if (o1.val == o2.val) return 0;
53+
else return 1;
54+
}
55+
});
56+
ListNode node = new ListNode(0), tmp = node;
57+
for (ListNode l : lists) {
58+
if (l != null) queue.add(l);
59+
}
60+
while (!queue.isEmpty()) {
61+
tmp.next = queue.poll();
62+
tmp = tmp.next;
63+
if (tmp.next != null) queue.add(tmp.next);
64+
}
65+
return node.next;
66+
}
67+
68+
public static void main(String[] args) {
69+
Solution solution = new Solution();
70+
ListNode.print(solution.mergeKLists(new ListNode[]{
71+
ListNode.createTestData("[1,3,5,7]"),
72+
ListNode.createTestData("[2,4,6]")
73+
}));
74+
}
75+
}

0 commit comments

Comments
 (0)