力扣-23题 合并K个升序链表(C++)- 链表

题目链接:https://leetcode-cn.com/problems/merge-k-sorted-lists/
题目如下:
在这里插入图片描述
在这里插入图片描述
解1、两两合并

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode* res=nullptr;
        if(lists.size()==0) return res;
        if(lists.size()==1) return lists[0];
        for(int i=0;i<lists.size();i++){
            res=mergetwoKLists(res,lists[i]);
        }
        return res;
    }
    ListNode* mergetwoKLists(ListNode* l1,ListNode* l2){
        if(!l1) return l2;
        if(!l2) return l1;
        ListNode* dummy=new ListNode(-1),*tail=dummy;
        while(l1&&l2){
            if(l1->val<l2->val){
                tail->next=l1;
                l1=l1->next;
            }else {
                tail->next=l2;
                l2=l2->next;
            }
            tail=tail->next;
        }
        if(l1) tail->next=l1;
        else if(l2) tail->next=l2;

        return dummy->next;
    }
};

解2、归并合并

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        return merge(lists,0,lists.size()-1);
    }
    ListNode* merge(vector<ListNode*>& lists,int l,int r){
        if(l==r) return lists[l];
        if(l>r) return nullptr;
        int mid=l+(r-l)/2;//(l+r)>>1;可以,不能l+(r-l)>>1,右移还是不太懂她

        return mergetwoKLists(merge(lists,l,mid),merge(lists,mid+1,r));
    }
    ListNode* mergetwoKLists(ListNode* l1,ListNode* l2){
        if(!l1) return l2;
        if(!l2) return l1;
        ListNode* dummy=new ListNode(-1),*tail=dummy;
        while(l1&&l2){
            if(l1->val<l2->val){
                tail->next=l1;
                l1=l1->next;
            }else {
                tail->next=l2;
                l2=l2->next;
            }
            tail=tail->next;
        }
        if(l1) tail->next=l1;
        else if(l2) tail->next=l2;

        return dummy->next;
    }
};

解3、小顶堆

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    //小顶堆做法
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        if(lists.size()==0) return nullptr;
        priority_queue<ListNode*,vector<ListNode*>,Cmp> minheap;

        for(auto e:lists){
            if(e) minheap.push(e);
        }

        auto dummy=new ListNode(-1);
        auto tail=dummy;
        while(!minheap.empty()){
            auto top=minheap.top();
            minheap.pop();
            tail->next=top;
            tail=tail->next;
            if(top->next) minheap.push(top->next);
        }
        return dummy->next;
    }
private:
    struct Cmp{
        bool operator()(ListNode* l1,ListNode* l2){
            return l1->val > l2->val; //目的:优先出列判定为!cmp,所以反向定义实现最小值优先
        }
    };
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值