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;
}
};
网友评论