Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
2.Queue<ListNode> minHeap = new PriorityQueue<ListNode>(size, Comparator)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
Queue<ListNode> minHeap = new PriorityQueue<ListNode>(lists.length, new Comparator<ListNode>(){
public int compare(ListNode l1, ListNode l2){
return l1.val - l2.val;
}
});
ListNode dummy = new ListNode(0);
ListNode prev = dummy;
for(ListNode l : lists){
if(l != null){
minHeap.offer(l);
}
}
while(!minHeap.isEmpty()){
ListNode cur = minHeap.poll();
prev.next = cur;
prev = prev.next;
if(cur.next != null)
minHeap.offer(cur.next);
}
return dummy.next;
}
}