public class Solution {
public ListNode mergeKLists(List<ListNode> lists) {
if(lists == null || lists.size() == 0) {
return null;
}
PriorityQueue<ListNode> q = new PriorityQueue<ListNode> (lists.size(), new Comparator<ListNode>() {
@Override
public int compare(ListNode a, ListNode b) {
return Integer.compare(a.val, b.val);
}
});
ListNode head = new ListNode(0);
ListNode cur = head;
for(ListNode l : lists) {
if(l != null) {
q.add(l);
}
}
while(!q.isEmpty()) {
ListNode temp = q.poll();
cur.next = temp;
if(temp.next != null){
q.add(temp.next);
}
cur = cur.next;
}
return head.next;
}
}