23. 合并K个排序链表

题目链接
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。

输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6

1. 顺序两两合并

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoList(ListNode* a,ListNode* b){
        ListNode* dummy=new ListNode(-1);
        ListNode* pre=dummy;
        while(a!=NULL&&b!=NULL){
            if(a->val>b->val){
                pre->next=b;
                b=b->next;
            }else{
                pre->next=a;
                a=a->next;
            }
            pre=pre->next;
        }
        if(a!=NULL){
            pre->next=a;
        }
        if(b!=NULL){
            pre->next=b;
        }
        return dummy->next;
    }

    ListNode* mergeKLists(vector<ListNode*>& lists) {
         ListNode* ans=NULL;
         for(int i=0;i<lists.size();i++){
             ans=mergeTwoList(ans,lists[i]);
         }
         return ans;
    }
};
  • 时间复杂度:假设每个链表的最长长度是 n。在第一次合并后,ans 的长度为n;第二次,ans长度2n…合并k次结束后,一共合并的长度是(1+2+…+k)*n。所以时间复杂度为O(k2 *n)
  • 空间复杂度:没有用到与 kk 和 nn 规模相关的辅助空间,故渐进空间复杂度为 O(1)

2. 分治合并

对上面方法优化,使用分治两两合并。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoList(ListNode* a,ListNode* b){
        ListNode* dummy=new ListNode(-1);
        ListNode* pre=dummy;
        while(a!=NULL&&b!=NULL){
            if(a->val>b->val){
                pre->next=b;
                b=b->next;
            }else{
                pre->next=a;
                a=a->next;
            }
            pre=pre->next;
        }
        if(a!=NULL){
            pre->next=a;
        }
        if(b!=NULL){
            pre->next=b;
        }
        return dummy->next;
    }

    ListNode* merge(vector<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));
    }

    ListNode* mergeKLists(vector<ListNode*>& lists) {
        return merge(lists,0,lists.size()-1);
    }
};
  • 时间复杂度:O(kn*logk)
  • 空间复杂度:O(logn)

3. 优先队列

队列中存放k个队列分别对应的最小值,然后从中取出整个的最小值。即队列始终维护k个数值。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    struct Node{
        int val;
        ListNode* node;
        Node(int v,ListNode* p){
            val=v;node=p;
        }
        bool operator < (const Node &a) const{
            return val>a.val;
        }
    };

    priority_queue<Node> qn;

    ListNode* mergeKLists(vector<ListNode*>& lists) {
        for(int i=0;i<lists.size();i++){
            if(lists[i]){
                qn.push(Node(lists[i]->val,lists[i]));
            }
        }
        ListNode* dummy=new ListNode(-1);
        ListNode* now=dummy;
        while(!qn.empty()){
            Node tmp=qn.top();qn.pop();
            now->next=tmp.node;
            now=now->next;
            if(tmp.node->next){
                qn.push(Node(tmp.node->next->val,tmp.node->next));
            }
        }
        return dummy->next;
    }
};
  • 时间复杂度:考虑优先队列中的元素不超过 k 个,那么插入和删除的时间代价为 O(logk),这里最多有 kn个点,对于每个点都被插入删除各一次,故总的时间代价即渐进时间复杂度为O(kn×logk)。
  • 空间复杂度:这里用了优先队列,优先队列中的元素不超过 k 个,故渐进空间复杂度为 O(k)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值