LeetCode23 合并k个升序链表

题目

给你一个链表数组,每个链表都已经按升序排列。

请你将所有链表合并到一个升序链表中,返回合并后的链表。

 示例 1: 
 输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
  1->4->5,
  1->3->4,
  2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6

 示例 2: 
 输入:lists = []
输出:[]

 示例 3: 
 输入:lists = [[]]
输出:[]

 提示: 
 k == lists.length 
 0 <= k <= 10^4 
 0 <= lists[i].length <= 500 
 -10^4 <= lists[i][j] <= 10^4 
 lists[i] 按 升序 排列 
 lists[i].length 的总和不超过 10^4 

方法

顺序合并

  • 时间复杂度: O(k2n),k为数组个数
  • 空间复杂度: O(1)
Js版本:
var mergeKLists = function(lists) {
    if(lists.length<=0) return null;
    function mergeTwoLists(list1,list2){
        if(list1==null) return list2;
        if(list2==null) return list1;
        if(list1.val<list2.val){
            list1.next = mergeTwoLists(list1.next,list2);
            return list1;
        }else{
            list2.next = mergeTwoLists(list2.next,list1);
            return list2;
        }
    }
    return lists.reduce((pre,next)=>mergeTwoLists(pre,next));
};

分治法

把数组用二分法,先讲数组分成两两结合,然后层层两两合并

  • 时间复杂度: O(kn×logk),k为数组个数
  • 空间复杂度: O(logk),二分法
Java版本:
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        return merge(lists,0,lists.length-1);
    }
    private ListNode merge(ListNode[] lists,int l,int r){
        if(l==r){
            return lists[l];
        }
        if(l>r){
            return null;
        }
        int mid = (l+r)/2;
        return mergeTwoList(merge(lists,l,mid),merge(lists,mid+1,r));
    }
    private ListNode mergeTwoList(ListNode node1,ListNode node2){
        ListNode head = new ListNode(),node = head;
        while(node1!=null&&node2!=null){
            if(node1.val<node2.val){
                node.next = node1;
                node1 = node1.next;
            }else{
                node.next = node2;
                node2 = node2.next;
            }
            node = node.next;
        }
        if(node1==null){
            node.next = node2;
        }
        if(node2 == null){
            node.next = node1;
        }
        return head.next;
    }
}
Js版本:
var mergeKLists = function(lists) {
    if(lists.length<=0) return null;
    function mergeTwoLists(list1,list2){
        if(list1==null) return list2;
        if(list2==null) return list1;
        if(list1.val<list2.val){
            list1.next = mergeTwoLists(list1.next,list2);
            return list1;
        }else{
            list2.next = mergeTwoLists(list2.next,list1);
            return list2;
        }
    }
    function merge(lists,l,r){
        if(l>r) return null;
        if(l===r) return lists[l];
	//特别注意,这里要用parseInt取整,不然为小数
        let mid = parseInt((l+r)/2);
        return mergeTwoLists(merge(lists,l,mid),merge(lists,mid+1,r));
    }
    return merge(lists,0,lists.length-1);
};

优先队列法

将数组的节点存入优先队列,优先队列会根据节点的值排序,然后取过值的节点往下找next,并继续存入队列中排序

  • 时间复杂度: O(kn×logk)
  • 空间复杂度: O(k),优先队列节点个数
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> priorityQueue = new PriorityQueue<ListNode>(new Comparator<ListNode>() {
            @Override
            public int compare(ListNode o1, ListNode o2) {
                return o1.val-o2.val;
            }
        });
        for(ListNode node:lists){
            if(node!=null){
                priorityQueue.offer(node);
            }
        }
        ListNode head = new ListNode();
        ListNode tail = head;
        while(!priorityQueue.isEmpty()){
            ListNode node = priorityQueue.poll();
            tail.next = node;
            tail = tail.next;
            if(node.next!=null){
                priorityQueue.offer(node.next);
            }
        }
        return head.next;
    }
}
posted @   你也要来一颗长颈鹿吗  阅读(21)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示