题解 | #合并k个已排序的链表#
合并k个已排序的链表
https://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6
Queue<ListNode> queue = new PriorityQueue<>(((o1, o2) -> o1.val-o2.val)); // 遍历链表 for (int i = 0; i < lists.size(); i++) { // 不为空则放入小顶堆 if (lists.get(i)!=null) { queue.offer(lists.get(i)); } } // 建立一个表头 ListNode res = new ListNode(-1); ListNode head = res; // 遍历链表元素 while (!queue.isEmpty()){ ListNode temp = queue.poll(); head.next = temp; head = head.next; // 遍历链表,将最小放入队列 if (temp.next!= null){ queue.add(temp.next); } } return res.next;