【leetcode】23. 合并K个升序链表

简介: 【leetcode】23. 合并K个升序链表
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists.length == 0) {
            return null;
        }
        ListNode head = new ListNode(0);
        ListNode curr = head;
        PriorityQueue<ListNode> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
        for (ListNode list : lists) {
            if (list == null) {
                continue;
            }
            pq.add(list);
        }
        while(!pq.isEmpty()) {
            ListNode nextNode = pq.poll();
            curr.next = nextNode;
            curr = curr.next;
            if (nextNode.next != null) {
                pq.add(nextNode.next);
            }
        }
        return head.next;
    }
}
相关文章
|
缓存 Java
【LeetCode】23. 合并K个升序链表
给你一个链表数组,每个链表都已经按升序排列。请你将所有链表合并到一个升序链表中,返回合并后的链表……
129 0
【LeetCode】第11天 - 23. 合并K个升序链表
【LeetCode】第11天 - 23. 合并K个升序链表
111 0
【LeetCode】第11天 - 23. 合并K个升序链表
|
6月前
|
算法
LeetCode第23题合并 K 个升序链表
这篇文章介绍了LeetCode第23题"合并K个升序链表"的解题方法,使用分而治之的思想,通过递归合并链表的方式解决了这个难题。
LeetCode第23题合并 K 个升序链表
|
9月前
|
Python
leetcode-23:合并K个排序链表
leetcode-23:合并K个排序链表
48 0
23_合并K个升序链表
23_合并K个升序链表
105 0
|
8月前
23.合并K个升序链表
23.合并K个升序链表
|
9月前
23. 合并 K 个升序链表
23. 合并 K 个升序链表
68 3
|
算法 测试技术 C++
C++算法:合并 K 个升序链表
C++算法:合并 K 个升序链表
【刷题记录】23. 合并K个升序链表
【刷题记录】23. 合并K个升序链表
211 0
【刷题记录】23. 合并K个升序链表

热门文章

最新文章