Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<algorithm>
- class Solution {
- public:
- ListNode* mergeKLists(vector<ListNode*>& lists) {
- if(lists.size()==0) return NULL;
- ListNode* prev = NULL ,*op1 =NULL;
- auto cmp = [](ListNode* a, ListNode* b) {
- return a->val > b->val;
- };
- priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> h(cmp);
- for(int i =0;i<lists.size();i++) {
- if(lists.at(i))
- h.push(lists.at(i));
- }
- while(!h.empty()) {
- if(prev==NULL)
- op1=prev=h.top();
- else {
- prev->next=h.top();
- prev=prev->next;
- }
- if(h.top()->next) {
- h.push(h.top()->next);
- }
- h.pop();
- }
- return op1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement