classSolution{ //pq,time O(nlogk), space O(K) public ListNode mergeKLists(ListNode[] lists){ if (lists.length == 0) returnnull; ListNode dummy = new ListNode(0); PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> a.val - b.val); for (int i = 0; i < lists.length; i++) if (lists[i] != null) pq.offer(lists[i]);//only first element will be insert to the pq ListNode cur = dummy; while(!pq.isEmpty()){ cur.next = pq.poll(); cur = cur.next; if (pq.isEmpty()) break; //last cycle dont need work if (cur.next != null) pq.offer(cur.next); } return dummy.next; } }