这里主要记录优先队列解法,除此之外,分治合并也值得学习和研究
public static ListNode mergeKLists3(ListNode[] lists) {
PriorityQueue<ListNode> priorityQueue = new PriorityQueue((o1, o2) -> {
ListNode o11 = (ListNode) o1;
ListNode o12 = (ListNode) o2;
return o11.val - o12.val;
});
for (ListNode list : lists) {
if (list != null){
priorityQueue.offer(list);
}
}
ListNode headNode = new ListNode();
ListNode tailNode = headNode;
while (!priorityQueue.isEmpty()){
ListNode poll = priorityQueue.poll();
tailNode.next = poll;
tailNode = poll;
if (poll.next != null){
priorityQueue.offer(poll.next);
}
}
return headNode.next;
}