forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesortedklists.java
More file actions
executable file
·62 lines (58 loc) · 1.52 KB
/
mergesortedklists.java
File metadata and controls
executable file
·62 lines (58 loc) · 1.52 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
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
class Node{
ListNode n;
int from;
Node(ListNode pp, int ff){
n = pp;
from = ff;
}
}
class MyCmp implements Comparator<Node>{
public int compare(Node n1, Node n2){
return n1.n.val-n2.n.val;
}
}
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
if(lists.size()==0){
return null;
}
if(lists.size()==1){
return lists.get(0);
}
ListNode res = new ListNode(0);
ListNode tail = res;
PriorityQueue<Node> pq = new PriorityQueue<Node>(lists.size(),new MyCmp());
ListNode p[] = new ListNode[lists.size()];
for(int i=0;i<lists.size();i++){
p[i] = lists.get(i);
}
for(int i=0;i<p.length;i++){
if(p[i]!=null){
pq.add(new Node(p[i],i));
}
}
while(!pq.isEmpty()){
Node tmp = pq.poll();
tail.next = tmp.n;
tail = tail.next;
p[tmp.from] = p[tmp.from].next;
if(p[tmp.from]!=null){
pq.add(new Node(p[tmp.from],tmp.from));
}
}
return res.next;
}
}