-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.合并k个升序链表.java
45 lines (37 loc) · 994 Bytes
/
23.合并k个升序链表.java
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
import java.util.PriorityQueue;
/*
* @lc app=leetcode.cn id=23 lang=java
*
* [23] 合并K个升序链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> queue = new PriorityQueue<>((v1, v2)->v1.val-v2.val);
for (ListNode list: lists) {
if (list != null ) queue.add(list);
}
ListNode ans = new ListNode(0);
ListNode temp = ans;
while (queue.size() > 0) {
ListNode t = queue.poll();
temp.next = t;
temp = t;
t = t.next;
temp.next = null;
if (t != null) queue.add(t);
}
return ans.next;
}
}
// @lc code=end