1、优先队列
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
import java.util.*;
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
Queue<ListNode> queue = new PriorityQueue<>((a, b) -> a.val - b.val);
if (lists == null || lists.size() == 0) return null;
for (ListNode head : lists) {
if (head == null) continue;
queue.offer(head);
}
ListNode dummy = new ListNode(0);
ListNode head = dummy;
while (!queue.isEmpty()) {
ListNode cur = queue.poll();
head.next = cur;
head = head.next;
if (cur.next != null) {
queue.offer(cur.next);
}
}
return dummy.next;
}
}