-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmergeKLists.java
76 lines (63 loc) · 1.81 KB
/
mergeKLists.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Divide and Conquer method
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
return merge(lists, 0, lists.length-1);
}
public ListNode merge(ListNode[] lists, int start, int end){
if(start == end) return lists[start];
else if(start < end){
int mid = start + (end-start)/2;
ListNode left = merge(lists, start, mid);
ListNode right = merge(lists, mid+1, end);
return mergeLists(left, right);
}
else
return null;
}
public ListNode mergeLists(ListNode l1, ListNode l2){
if(l1 == null) return l2;
else if(l2 == null) return l1;
else if(l1.val < l2.val){
l1.next = mergeLists(l1.next, l2);
return l1;
}
else {
l2.next = mergeLists(l1, l2.next);
return l2;
}
}
}
// Priority Queue
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode dummy = new ListNode(0), cur = dummy;
if(lists.length == 0)
return null;
PriorityQueue<ListNode> minHeap = new PriorityQueue<>(lists.length, new Comparator<ListNode>(){
public int compare(ListNode l1, ListNode l2)
{
return l1.val - l2.val;
}
});
for(ListNode list : lists)
if(list != null)
minHeap.offer(list);
while(!minHeap.isEmpty())
{
ListNode temp = minHeap.poll();
cur.next = temp;
if(temp.next != null)
minHeap.offer(temp.next);
cur = temp;
}
return dummy.next;
}
}