-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack23.java
More file actions
49 lines (46 loc) · 1.3 KB
/
Stack23.java
File metadata and controls
49 lines (46 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package stack;
import utils.ListNode;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
import java.util.PriorityQueue;
/**
* @ProjectName: leetcode
* @Package: stack
* @ClassName: Stack23
* @Author: markey
* @Description:23. 合并K个排序链表
* 合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
*
* 示例:
*
* 输入:
* [
* 1->4->5,
* 1->3->4,
* 2->6
* ]
* 输出: 1->1->2->3->4->4->5->6
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/4/26 21:31
* @Version: 1.0
*/
public class Stack23 {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> heap = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
Arrays.stream(lists).filter(Objects::nonNull).forEach(node -> heap.offer(node));
ListNode preHead = new ListNode(0);
ListNode head = preHead;
while (!heap.isEmpty()) {
head.next = heap.poll();
head = head.next;
if (head.next != null) {
heap.offer(head.next);
}
}
return preHead.next;
}
}