题目
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
题意
合并k个有序链表成1个有序链表
分析
将合并k个成1个链表 分解为: k个链表两两合并, 之后得到的k’链表再两两合并,直到最后只剩下一个链表
实现
class Solution {
public:
ListNode *mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode dummy(-1);
ListNode *result = &dummy;
while(l1 && l2) {
if(l1->val <= l2->val) {
result->next = l1;
result = result->next;
l1 = l1->next;
}
else {
result->next = l2;
result = result->next;
l2 = l2->next;
}
}
if(l1)
result->next = l1;
else if(l2)
result->next = l2;
return dummy.next;
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
if (lists.empty()) return NULL;
int len = lists.size();
while (len > 1) {
for (int i = 0; i < len / 2; ++i) {
lists[i] = mergeTwoLists(lists[i], lists[len - 1 - i]);
}
len = (len + 1) / 2;
}
return lists.front();
}
};