力扣 23. 合并 K 个升序链表

题目链接

23. 合并 K 个升序链表

题目描述

思路

最小堆(优先队列)q存储每个链表的表头的值,通过字典m存储值对应的链表节点(字典的值的类型为vector,因为可能出现值相同的情况)。
每次取q中最小的值,找到对应的链表节点,加入结果链表数组中,然后从q和m中删除值和节点。
若该节点存在下一节点的话将下一节点存入q以及m。
直到q为空,结束循环。

代码

/**
 * 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())return nullptr;
        ListNode *start=nullptr;
        ListNode *now=nullptr;
        ListNode *temp=nullptr;
        int temp_val;
        map<int,vector<ListNode*>> num2node;

        priority_queue<int,vector<int>,greater<int>> min_heap;
        for(auto &list:lists){
            if(list==nullptr)continue;
        min_heap.emplace(list->val);
        // if(num2node.find(list->val)==num2node.end()){
        //     num2node[list->val]=vector<ListNode*>();
        // }
        num2node[list->val].push_back(list);
        }
        while(!min_heap.empty()){
            temp_val=min_heap.top();
            // cout<<temp_val<<" ";
            min_heap.pop();
            temp=num2node[temp_val].back();
            num2node[temp_val].pop_back();
            if(now==nullptr){
                start=temp;
                now=temp;
            }else{
                now->next=temp;
                now=now->next;
            }
            if(temp->next==nullptr)continue;
            // cout<<temp->next->val;
            min_heap.emplace(temp->next->val);
            // if(num2node.find(temp->next->val)==num2node.end()){
            //     num2node[temp->next->val]=vector<ListNode*>();
            // }
            num2node[temp->next->val].push_back(temp->next);
        
            // cout<<endl;
        }
            return start;
    }
};

结果

优化

用最小堆同时存储值和节点(通过struct实现),这样省去了字典的操作。
下为官方题解:

class Solution {
public:
    struct Status {
        int val;
        ListNode *ptr;
        bool operator < (const Status &rhs) const {
            return val > rhs.val;
        }
    };

    priority_queue <Status> q;

    ListNode* mergeKLists(vector<ListNode*>& lists) {
        for (auto node: lists) {
            if (node) q.push({node->val, node});
        }
        ListNode head, *tail = &head;
        while (!q.empty()) {
            auto f = q.top(); q.pop();
            tail->next = f.ptr; 
            tail = tail->next;
            if (f.ptr->next) q.push({f.ptr->next->val, f.ptr->next});
        }
        return head.next;
    }
};

作者:力扣官方题解
链接:https://leetcode.cn/problems/merge-k-sorted-lists/solutions/219756/he-bing-kge-pai-xu-lian-biao-by-leetcode-solutio-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值