/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param lists ListNode类vector
* @return ListNode类
*/
ListNode* mergeKLists(vector<ListNode*>& lists) {
// write code here
// 利用multiset
multiset<int> m_s;
for(auto list:lists)
{
while(list)
{
m_s.emplace(list->val);
list = list->next;
}
}
// 创建链表
ListNode* ans = new ListNode(-1);
ListNode* cur = ans;
for(auto it=m_s.begin(); it!=m_s.end(); ++it)
{
ListNode* temp = new ListNode(*it);
cur->next = temp;
cur = cur->next;
}
return ans->next;
}
};