Skip to content

Instantly share code, notes, and snippets.

@hayunjong83
Last active December 24, 2019 13:22
Show Gist options
  • Save hayunjong83/673252b6d5c67d124d5eb348b78902be to your computer and use it in GitHub Desktop.
Save hayunjong83/673252b6d5c67d124d5eb348b78902be to your computer and use it in GitHub Desktop.
struct Compare
{
bool operator() (ListNode* a, ListNode* b){
return a->val > b->val;
}
};
class Solution{
public:
ListNode* mergeKLists( vector<ListNode*>& lists){
priority_queue< ListNode*, vector<ListNode*>, Compare> pq;
for(int i=0; i < lists.size() ; i++){
if(lists[i] !=NULL)
pq.push(lists[i]);
}
ListNode head(0);
ListNode* cur = &head;
while(!pq.empty()){
cur->next = pq.top();
cur = cur->next;
pq.pop();
if(cur->next !=NULL)
pq.push(cur->next);
}
return head.next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment