-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.合并k个排序链表.java
54 lines (49 loc) · 1.29 KB
/
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
45
46
47
48
49
50
51
52
53
import java.util.Comparator;
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(int x) { val = x; }
// * }
*/
// public class ListNode {
// int val;
// ListNode next;
// ListNode(int x) { val = x; }
// }
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
// if(lists.length==0) return null;
ListNode dummy = new ListNode(0);
PriorityQueue pq = new PriorityQueue<ListNode>(new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
// TODO Auto-generated method stub
return o1.val-o2.val;
}
});
for(int i=0;i<lists.length;i++){
if(lists[i]!=null)
pq.add(lists[i]);
}
ListNode p = dummy;
while (!pq.isEmpty()) {
ListNode temp = (ListNode)pq.poll();
// System.out.println(temp.val);
p.next = temp;
p = p.next;
if(p.next!=null)
pq.add(p.next);
}
return dummy.next;
}
}
// @lc code=end