第一次自己的实现(比较耗时270ms左右)
ListNode* mergeKLists1(vector<ListNode*>& lists)
{
if (lists.size() < 1)
return NULL;
ListNode *pre = NULL, *tmp = NULL, *rs = NULL;
ListNode *l1 = NULL, *l2 = NULL ;
int i = 0;
while (i < lists.size())
{
if (lists[i] == NULL)
{
i++;
}
else
{
break;
}
}
if (i == lists.size())
{
return NULL;
}
rs = lists[i++];
for ( ; i < lists.size(); i++)
{
l1 = rs;
l2 = lists[i];
pre = NULL;
while (l1 != NULL && l2 != NULL)
{
if (l1->val <= l2->val)
{
pre = l1;
l1 = l1->next;
}
else
{
tmp = l2->next;
if (pre == NULL)
{
l2->next = l1;
pre = rs = l1 = l2;
l1 = l1->next;
}
else
{
pre->next = l2;
pre = pre->next;
l2->next = l1;
}
l2 = tmp;
}
}
if (l1 == NULL && pre != NULL)
{
pre->next = l2;
}
}
return rs;
}
leetcode比较优秀的答案(耗时75ms左右)
ListNode *mergeKLists(vector<ListNode *> &lists) {
if(lists.empty()){
return nullptr;
}
while(lists.size() > 1){
lists.push_back(mergeTwoLists(lists[0], lists[1]));
lists.erase(lists.begin());
lists.erase(lists.begin());
}
return lists.front();
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(l1 == nullptr){
return l2;
}
if(l2 == nullptr){
return l1;
}
if(l1->val <= l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
leetcode 还有更优秀的(44ms 左右)
struct compare {
bool operator ()(const ListNode *l1, const ListNode *l2) {
return l1->val > l2->val;
}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
int length = lists.size();
ListNode* cur = new ListNode(0);
ListNode* hhead = cur;
priority_queue<ListNode*, vector<ListNode*>, compare> pq;
for (int i = 0; i<length; i++){
if (lists[i] != NULL) pq.push(lists[i]);
}
while (!pq.empty()){
cur->next = pq.top();
pq.pop();
cur = cur->next;
if (cur->next != NULL){
pq.push(cur->next);
}
}
return hhead->next;
}
自己的第二次实现完整例子 (还可以用时70ms以内)
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* createList(ListNode*head, int num)
{
ListNode* pl = head;
ListNode* node = new ListNode(num);
if (pl == NULL)
{
head = node;
}
else
{
while (pl->next != NULL)
{
pl = pl->next;
}
pl->next = node;
}
return head;
}
ListNode* mergeKLists(vector<ListNode*>& lists)
{
if (lists.size() < 1)
return NULL;
ListNode *pre = NULL, *tmp = NULL, *rs = NULL;
ListNode *l1 = NULL, *l2 = NULL;
while (lists.size() > 1)
{
if (lists[0] == NULL)
{
lists.erase(lists.begin());
continue;
}
rs = l1 = lists[0];
if (lists[1] == NULL)
{
lists.erase(lists.begin()+1);
continue;
}
else
{
l2 = lists[1];
pre = NULL;
while (l1 != NULL && l2 != NULL)
{
if (l1->val <= l2->val)
{
pre = l1;
l1 = l1->next;
}
else
{
tmp = l2->next;
if (pre == NULL)
{
l2->next = l1;
pre = rs = l1 = l2;
l1 = l1->next;
}
else
{
pre->next = l2;
pre = pre->next;
l2->next = l1;
}
l2 = tmp;
}
}
if (l1 == NULL && pre != NULL)
{
pre->next = l2;
}
lists.push_back(rs);
lists.erase(lists.begin());
lists.erase(lists.begin());
}
}
return lists[0];
}
int main()
{
int arr[] = { 1, 7, 12, 26, 39 };
int arr1[] = { 2, 15, 27, 28, 30 };
int arr2[] = { 13, 25, 37, 38, 50 };
int arr3[] = { 14, 23, 24, 34, 60 };
ListNode* head1 = NULL, *head2 = NULL, *head3 = NULL, *head4 = NULL, *rs = NULL;
for (int i = 0; i < 5; i++)
{
head1 = createList(head1, arr[i]);
head2 = createList(head2, arr1[i]);
head3 = createList(head3, arr2[i]);
head4 = createList(head4, arr3[i]);
}
vector<ListNode*> lists;
lists.push_back(head1);
lists.push_back(head2);
lists.push_back(head3);
lists.push_back(head4);
rs = mergeKLists(lists);
int i = 0;
while (rs)
{
i++;
cout << rs->val << " ";
if (i % 5 == 0)
cout << endl;
rs = rs->next;
}
system("pause");
return 0;
}