Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created November 10, 2018 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/f9e4c4053fa01a1455e013d1126ab531 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/f9e4c4053fa01a1455e013d1126ab531 to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int len_l = lists.size();
ListNode head(0);
ListNode *h = &head;
auto comp = [](ListNode* l1, ListNode* l2){
return l1->val> l2->val;
};
priority_queue<ListNode*, std::vector<ListNode*>, decltype(comp)> q(comp);
for(int i = 0; i < len_l; i++){
if(lists[i])
q.push(lists[i]);
}
while(q.empty() == false){
h->next = q.top();
q.pop();
h = h->next;
if(h->next != NULL)
q.push(h->next);
}
return head.next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment