class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0) {
return null;
}
ListNode head = new ListNode(0);
ListNode curr = head;
PriorityQueue<ListNode> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
for (ListNode list : lists) {
if (list == null) {
continue;
}
pq.add(list);
}
while(!pq.isEmpty()) {
ListNode nextNode = pq.poll();
curr.next = nextNode;
curr = curr.next;
if (nextNode.next != null) {
pq.add(nextNode.next);
}
}
return head.next;
}
}
【leetcode】23. 合并K个升序链表
最新推荐文章于 2024-10-07 07:09:00 发布