Guest User

Untitled

a guest
Apr 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /**
  5. * Definition for singly-linked list.
  6. * struct ListNode {
  7. * int val;
  8. * ListNode *next;
  9. * ListNode(int x) : val(x), next(NULL) {}
  10. * };
  11. */
  12. struct ListNode {
  13. int val;
  14. ListNode *next;
  15. ListNode(int x) : val(x), next(NULL) {}
  16. };
  17. class Solution {
  18. public:
  19. ListNode* mergeKLists(vector<ListNode*>& lists) {
  20. priority_queue<pair<int, ListNode*>> pq;
  21. for(auto &x : lists)
  22. pq.push({x->val, x});
  23. ListNode *res = new ListNode(0), *head = res;
  24. while(!pq.empty()){
  25. auto x = pq.top();
  26. pq.pop();
  27. res = res->next = new ListNode(x.first);
  28. if(x.second->next)
  29. pq.push({x.second->next->val, x.second->next});
  30. }
  31. return head->next;
  32. }
  33. };
  34.  
  35. int main(){
  36. return 0;
  37. }
Add Comment
Please, Sign In to add comment