public static ListNode mergeKLists(List<ListNode> lists) {  
        // write your code here
        ListNode dummy=new ListNode(),p=dummy;
        Queue<ListNode>q=new PriorityQueue<>((a,b)->a.val-b.val);
        for (ListNode e:lists) {
            if (e!=null)
                q.offer(e);
        }
        while (!q.isEmpty()) {
            p=p.next=q.poll();
            if (p.next!=null)
                q.offer(p.next);
        }
        return dummy.next;
    }