题解 | #合并k个已排序的链表#
合并k个已排序的链表
https://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6
//使用大根堆解决
public ListNode mergeKLists(ArrayList<ListNode> lists) {
PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
ListNode head = new ListNode(-1);
for (ListNode node : lists) {
while(node != null) {
pq.offer(node.val);
node = node.next;
}
}
while(!pq.isEmpty()) {
ListNode newNode = new ListNode(pq.poll());
newNode.next = head.next;
head.next = newNode;
}
return head.next;
}


