PDF版 ePub版

# Merge k Sorted Lists（合并 K 个已排序链表）

## 原文

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

## 代码

mid = (start + end) / 2
start -- mid                    （1）
mid + 1 -- end                  （2）

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*> &lists) {
return partition(lists, 0, lists.size() - 1);
}

ListNode* partition(vector<ListNode*>& lists, int start, int end) {
if(start == end) {
return lists[start];
}

if(start < end) {
int mid = (start + end) / 2;
ListNode* l1 = partition(lists, start, mid);
ListNode* l2 = partition(lists, mid + 1, end);
return mergeTwoLists(l1, l2);
}
return NULL;
}

ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l2 == NULL) return l1;
if(l1 == NULL) return l2;

if(l1->val > l2->val) {
ListNode* temp = l2;
temp->next = mergeTwoLists(l1, l2->next);
return temp;
} else {
ListNode* temp = l1;
temp->next = mergeTwoLists(l1->next, l2);
return temp;
}
}
};

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*> &lists) {
int size = lists.size();
if(size == 0) return NULL;
if(size == 1) return lists[0];

int i = 2, j;
while(i / 2 < size) {
for(j = 0; j < size; j += i) {
ListNode* p = lists[j];
if(j + i / 2 < size) {
p = mergeTwoLists(p, lists[j + i / 2]);
lists[j] = p;
}
}
i *= 2;
}
return lists[0];
}

ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l2 == NULL) return l1;
if(l1 == NULL) return l2;

if(l1->val > l2->val) {
ListNode* temp = l2;
temp->next = mergeTwoLists(l1, l2->next);
return temp;
} else {
ListNode* temp = l1;
temp->next = mergeTwoLists(l1->next, l2);
return temp;
}
}
};