-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergerkSortedLists.java
51 lines (47 loc) · 1.26 KB
/
MergerkSortedLists.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
/**
* @author chenqun
* Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
* k lists,each list has n elements,the complexity is O(knlogk)
*/
import java.util.Comparator;
import java.util.PriorityQueue;
public class MergerkSortedLists {
public ListNode mergeKLists(ListNode[] lists) {
if(lists.length == 0)
return null;
ListNode head = new ListNode(0);
ListNode p = head;
//自己实现comrarator
Comparator<ListNode> order = new Comparator<ListNode>(){
public int compare(ListNode a, ListNode b){
if(a.val > b.val)
return 1;
else if(a.val == b.val)
return 0;
else
return -1;
}
};
PriorityQueue<ListNode> q = new PriorityQueue<ListNode>(lists.length, order);
for(ListNode list : lists){
if(list!=null)
q.add(list);
}
while(q.size() > 0){
ListNode temp = q.poll();
p.next = temp; //每次把最小的加进去
if(temp.next !=null)
q.add(temp.next);
p = p.next;
}
return head.next;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode[] lists = {l1,l2};
MergerkSortedLists test = new MergerkSortedLists ();
System.out.println(test.mergeKLists(lists));
}
}