【优先队列】LeetCode 23. 合并K个升序链表
题目链接
思路
把全部结点放入优先队列中,然后再依次组成新链表
代码
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<Integer> listNodePriorityQueue = new PriorityQueue<>();
for(int i = 0; i < lists.length; i++){
ListNode p = lists[i];
while(p != null){
listNodePriorityQueue.add(p.val);
p = p.next;
}
}
ListNode res = new ListNode();
ListNode tail = res;
while(!listNodePriorityQueue.isEmpty()){
tail.next = new ListNode(listNodePriorityQueue.poll());
tail = tail.next;
}
return res.next;
}
}