/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists.length == 0) return null;
PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length,new Comparator<ListNode>(){
public int compare(ListNode o1,ListNode o2){
return o1.val>o2.val?1:o1.val==o2.val?0:-1;
}
});
for(ListNode temp:lists){
if(temp!=null){
queue.add(temp);
}
}
ListNode dump = new ListNode(0);
ListNode tail = dump;
while(!queue.isEmpty()){
tail.next = queue.poll();
tail = tail.next;
if(tail.next != null){
queue.add(tail.next);
}
}
return dump.next;
}
}