/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
#include <cfloat>
#include <memory>
#include <queue>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param lists ListNode类vector
* @return ListNode类
*/
struct Comapre
{
bool operator()(struct ListNode* l1,struct ListNode* l2)
{
return l1->val > l2->val;
}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<struct ListNode*,vector<struct ListNode*>,Comapre> Q;
for(auto& l : lists)
{
if(l != nullptr)
Q.push(l);
}
auto* dummy = new struct ListNode(-1);
auto* ptr = dummy;
while(Q.size())
{
ptr ->next = Q.top(); ptr = ptr ->next;
if(Q.top() ->next != nullptr)
Q.push(Q.top() ->next);
Q.pop();
}
ptr = dummy ->next;
delete dummy;
return ptr;
}
};