力扣链接:合并K个升序链表
代码package com.harrison.class06;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
public class Code06_MergeKSortedLists {
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public static ListNode mergeKLists(ListNode[] lists) {
if (lists == null) {
return null;
}
PriorityQueue heap = new PriorityQueue<>(new ListNodeComparator());
for (int i = 0; i < lists.length; i++) {
if(lists[i]!=null){
heap.add(lists[i]);
}
}
if (heap.isEmpty()) {
return null;
}
ListNode head = heap.poll();
ListNode pre = head;
if (pre.next != null) {
heap.add(pre.next);
}
while(!heap.isEmpty()){
ListNode cur=heap.poll();
pre.next=cur;
pre=cur;
if(cur.next!=null){
heap.add(cur.next);
}
}
return head;
}
public static class ListNodeComparator implements Comparator {
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
}
}



