classSolution {publicListNodemergeKLists(ListNode[] lists) {PriorityQueue<ListNode> pq =newPriorityQueue<>((a, b) ->a.val-b.val);// adding all the starting element from each list to PQfor (int i =0; i <lists.length; i++) {if (lists[i] !=null) {pq.add(lists[i]); } }// Main workif (pq.peek() !=null) {ListNode ans =newListNode(0);ListNode ansPointer = ans;while (pq.size() !=0) {ListNode t =pq.poll();ansPointer.next= t; ansPointer =ansPointer.next;if (t.next!=null) {pq.add(t.next); } }returnans.next; } else {returnnull; } }}