美文网首页
merge-k-sorted-lists

merge-k-sorted-lists

作者: DaiMorph | 来源:发表于2019-06-25 00:19 被阅读0次
    class Solution {
    public:
        struct cmp{
            bool operator()(const ListNode*a,const ListNode*b){
                return a->val>b->val;
            }
        };
        
        ListNode *mergeKLists(vector<ListNode *> &lists) {
            priority_queue<ListNode*,vector<ListNode*>,cmp>q;
            for(auto l:lists)if(l)q.push(l);
            ListNode*dummy=new ListNode(-1);
            ListNode*head=dummy;
            while(!q.empty())
            {
                ListNode*top=q.top();
                q.pop();
                head->next=top;
                head=head->next;
                if(top->next)q.push(top->next);
            }
            return dummy->next;
        }
    };
    

    相关文章

      网友评论

          本文标题:merge-k-sorted-lists

          本文链接:https://www.haomeiwen.com/subject/hjczqctx.html