1public ListNode mergeKLists(ListNode[] lists) {
2 PriorityQueue<ListNode> heap = new PriorityQueue<>((a, b) -> a.val - b.val);
3
4 // Add the head of each list to the min-heap
5 for (ListNode list : lists) {
6 if (list != null) {
7 heap.offer(list);
8 }
9 }
10
11 ListNode dummy = new ListNode(0);
12 ListNode current = dummy;
13
14 // Continuously extract the minimum element
15 while (!heap.isEmpty()) {
16 ListNode node = heap.poll();
17 current.next = node;
18 current = current.next;
19
20 if (node.next != null) {
21 heap.offer(node.next);
22 }
23 }
24
25 return dummy.next;
26}