Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
方法1 借助堆
bool cmp (ListNode * l1, ListNode *l2)
{
return l1->val > l2->val;
}
//借助堆, 每次new
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
//vector<ListNode *> vec(lists.begin(), lists.end());//这样子不对,有可能lists里面有空指针
vector<ListNode *> vec;
for (int i=0;i<lists.size();i++)
{
if (NULL != lists[i])
vec.push_back(lists[i]);
}
ListNode * head = new ListNode (-1);
ListNode * pre = head;
make_heap(vec.begin(), vec.end(), cmp);
while(!vec.empty())
{
ListNode * tmp = vec[0];
pop_heap(vec.begin(), vec.end(),cmp);
vec.pop_back();
pre->next = new ListNode (tmp->val);
pre = pre->next;
if (NULL != tmp->next)
{
vec.push_back(tmp->next);
push_heap(vec.begin(), vec.end(),cmp);
}
}
return head->next;
}
};
//借助堆 不new
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
vector<ListNode *> vec;
for (int i=0;i<lists.size();i++)
{
if (NULL != lists[i])
vec.push_back(lists[i]);
}
ListNode * head = new ListNode (-1);
ListNode * pre = head;
make_heap(vec.begin(), vec.end(), cmp);
while(!vec.empty())
{
ListNode * tmp = vec[0];
ListNode * tmpnext = tmp->next;
pop_heap(vec.begin(), vec.end(),cmp);
vec.pop_back();
pre->next = tmp;
pre = pre->next;
if (NULL != tmpnext)
{
vec.push_back(tmpnext);
push_heap(vec.begin(), vec.end(),cmp);
}
}
return head->next;
}
};
//两两合并
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
if (lists.size() == 0)
return NULL;
while (lists.size()>1)
{
vector<ListNode *> vec;
for (int i=0; i<lists.size(); i++)
{
if(i%2 == 1)
vec.push_back(mergeTwoLists(lists[i-1], lists[i]));
}
if (lists.size()%2 == 1)
vec.push_back(*(lists.end()-1));
lists.swap(vec);
}
return lists[0];
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
ListNode * tmp1 = l1;
ListNode * tmp2 = l2;
ListNode * Head = new ListNode(0);
ListNode * tmp1_pre = Head;
Head->next = l1;
while(tmp2 != NULL&&tmp1 != NULL)
{
if (tmp2->val > tmp1->val)
{
tmp1_pre = tmp1;
tmp1 = tmp1->next;
}
else
{
ListNode * tmp = tmp2->next;
tmp1_pre->next = tmp2;
tmp2->next = tmp1;
tmp2 = tmp;
tmp1_pre = tmp1_pre->next;
}
}
if (tmp1 == NULL)
{
tmp1_pre->next = tmp2;
}
return Head->next;
}
};
方法2 借助之前的merge-2-sorted-lists 两两合并
//两两合并
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
if (lists.size() == 0)
return NULL;
while (lists.size()>1)
{
vector<ListNode *> vec;
for (int i=0; i<lists.size(); i++)
{
if(i%2 == 1)
vec.push_back(mergeTwoLists(lists[i-1], lists[i]));
}
if (lists.size()%2 == 1)
vec.push_back(*(lists.end()-1));
lists.swap(vec);
}
return lists[0];
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
ListNode * tmp1 = l1;
ListNode * tmp2 = l2;
ListNode * Head = new ListNode(0);
ListNode * tmp1_pre = Head;
Head->next = l1;
while(tmp2 != NULL&&tmp1 != NULL)
{
if (tmp2->val > tmp1->val)
{
tmp1_pre = tmp1;
tmp1 = tmp1->next;
}
else
{
ListNode * tmp = tmp2->next;
tmp1_pre->next = tmp2;
tmp2->next = tmp1;
tmp2 = tmp;
tmp1_pre = tmp1_pre->next;
}
}
if (tmp1 == NULL)
{
tmp1_pre->next = tmp2;
}
return Head->next;
}
};
测试
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
bool cmp (ListNode * l1, ListNode *l2)
{
return l1->val > l2->val;
}
ListNode * generateLinkList(vector<int> vec)
{
ListNode * head = NULL;
if (vec.size() == 0)
return head;
head = new ListNode(vec[0]);
ListNode * tmp = head;
for (int i = 1; i<vec.size(); i++)
{
tmp->next = new ListNode(vec[i]);
tmp = tmp->next;
}
return head;
}
void print(ListNode * head)
{
while(head != NULL)
{
cout<<head->val<<" ";
head = head->next;
}
cout<<endl;
}
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
vector<ListNode *> vec;
for (int i=0;i<lists.size();i++)
{
if (NULL != lists[i])
vec.push_back(lists[i]);
}
ListNode * head = new ListNode (-1);
ListNode * pre = head;
make_heap(vec.begin(), vec.end(), cmp);
while(!vec.empty())
{
ListNode * tmp = vec[0];
pop_heap(vec.begin(), vec.end(),cmp);
vec.pop_back();
pre->next = new ListNode (tmp->val);
pre = pre->next;
if (NULL != tmp->next)
{
vec.push_back(tmp->next);
push_heap(vec.begin(), vec.end(),cmp);
}
}
return head->next;
}
};
class Solution1 {
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
if (lists.size() == 0)
return NULL;
while (lists.size()>1)
{
vector<ListNode *> vec;
for (int i=0; i<lists.size(); i++)
{
if(i%2 == 1)
vec.push_back(mergeTwoLists(lists[i-1], lists[i]));
}
if (lists.size()%2 == 1)
vec.push_back(*(lists.end()-1));
lists.swap(vec);
}
return lists[0];
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
ListNode * tmp1 = l1;
ListNode * tmp2 = l2;
ListNode * Head = new ListNode(0);
ListNode * tmp1_pre = Head;
Head->next = l1;
while(tmp2 != NULL&&tmp1 != NULL)
{
if (tmp2->val > tmp1->val)
{
tmp1_pre = tmp1;
tmp1 = tmp1->next;
}
else
{
ListNode * tmp = tmp2->next;
tmp1_pre->next = tmp2;
tmp2->next = tmp1;
tmp2 = tmp;
tmp1_pre = tmp1_pre->next;
}
}
if (tmp1 == NULL)
{
tmp1_pre->next = tmp2;
}
return Head->next;
}
};
//借助堆 不new
class Solution3 {
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
vector<ListNode *> vec;
for (int i=0;i<lists.size();i++)
{
if (NULL != lists[i])
vec.push_back(lists[i]);
}
ListNode * head = new ListNode (-1);
ListNode * pre = head;
make_heap(vec.begin(), vec.end(), cmp);
while(!vec.empty())
{
ListNode * tmp = vec[0];
ListNode * tmpnext = tmp->next;
pop_heap(vec.begin(), vec.end(),cmp);
vec.pop_back();
pre->next = tmp;
pre = pre->next;
if (NULL != tmpnext)
{
vec.push_back(tmpnext);
push_heap(vec.begin(), vec.end(),cmp);
}
}
return head->next;
}
};
int main()
{
Solution s;
int A[] = {-1,1,2,3,4,5,6};
vector<int> vec(A, A +7);
sort(vec.begin(), vec.end());
ListNode * head1 = generateLinkList(vec);
int B[] = {-5,-4, 7,8,9};
vector<int> vec1(B, B +5);
sort(vec1.begin(), vec1.end());
ListNode * head2 = generateLinkList(vec1);
int C[] = {-9,22,33};
vector<int> vec2(C, C +3);
sort(vec2.begin(), vec2.end());
ListNode * head3 = generateLinkList(vec2);
vector<ListNode *> veclist;
veclist.push_back(head1);
veclist.push_back(head2);
veclist.push_back(head3);
ListNode * head = s.mergeKLists(veclist);
print(head);
}