Advertisement
srinathsv

Untitled

Sep 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include<algorithm>
  2. class Solution {
  3. public:
  4. ListNode* mergeKLists(vector<ListNode*>& lists) {
  5. if(lists.size()==0) return NULL;
  6. ListNode* prev = NULL ,*op1 =NULL;
  7. auto cmp = [](ListNode* a, ListNode* b) {
  8. return a->val > b->val;
  9. };
  10. priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> h(cmp);
  11. for(int i =0;i<lists.size();i++) {
  12. if(lists.at(i))
  13. h.push(lists.at(i));
  14. }
  15.  
  16. while(!h.empty()) {
  17. if(prev==NULL)
  18. op1=prev=h.top();
  19. else {
  20. prev->next=h.top();
  21. prev=prev->next;
  22. }
  23. if(h.top()->next) {
  24. h.push(h.top()->next);
  25. }
  26. h.pop();
  27. }
  28. return op1;
  29. }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement