static int __ = [](){
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// log(n)*n;
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.size() <= 0)
return NULL;
ListNode* res = new ListNode(0);
ListNode* list = res;
priority_queue<int,vector<int>,greater<int>> q;
for (int i = 0; i < lists.size(); i++){
ListNode* index = lists[i];
while(index != NULL){
q.push(index->val);
index = index->next;
}
}
while(!q.empty())
list->next = new ListNode(q.top()), list = list->next, q.pop();
return res->next;
}
};
LetCode 23. 合并K个排序链表
最新推荐文章于 2024-06-21 10:00:00 发布