Last active
December 24, 2019 13:22
-
-
Save hayunjong83/673252b6d5c67d124d5eb348b78902be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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