class Solution {
public:
static bool comp(ListNode * a , ListNode * b){
return a->val > b->val;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode head(0);
ListNode *curNode = &head;
vector<ListNode*> v;
for(int i=0;i<lists.size();i++){
if(lists[i]) v.push_back(lists[i]);
}
make_heap(v.begin(),v.end(),comp);
while(v.size()>0){
curNode->next = v.front();
pop_heap(v.begin(),v.end(),comp);
v.pop_back();
curNode = curNode->next;
if(curNode->next){
v.push_back(curNode->next);
push_heap(v.begin(),v.end(),comp);
}
}
return head.next;
}
};
leetcode 23. Merge k Sorted Lists
最新推荐文章于 2019-05-23 22:12:49 发布