23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input: [ 1->4->5, 1->3->4, 2->6 ] Output: 1->1->2->3->4->4->5->6
题目思路很清楚:将多个已经排序好的链表合并成一个有序的链表。这是一道很经典的题目,在分布式系统中有很广泛的应用,也经常出现在各类的面试题中。这道题的解法并不单一,有很大的开放性,下面是我完成该题目的几种思路,如有错误请多指教,非常感谢!
思路一:
参考归并排序的思路,对k个链表进行分组,最后两两归并,最终合成一个链表,具体代码实现如下所示
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0) return NULL;
return helper(lists, 0, lists.size()-1);
}
ListNode* helper(vector<ListNode*>& lists, int left, int right) {
if(left < right) {
int mid = (left + right)/2;
return merge(helper(lists, left, mid), helper(lists, mid+1, right));
}
return lists[left];
}
ListNode* merge(ListNode * l1, ListNode * l2) {
ListNode * dummy = new ListNode(0);
ListNode * cur = dummy;
while(l1 != NULL && l2 != NULL) {
if(l1->val < l2->val) {
cur->next = l1;
l1 = l1->next;
cur = cur->next;
}
else {
cur->next = l2;
l2 = l2->next;
cur = cur->next;
}
}
if(l1 != NULL)
cur->next = l1;
else
cur->next = l2;
cur = dummy->next;
delete dummy;
return cur;
}
};时间复杂度为O(nklognk), 空间复杂度为O(logk)
思路二:使用优先队列(priority_queue)实现堆,初始时将每个链表的第一个结点加入堆中,之后每次弹出堆顶元素,将这个结点指向的下一结点加入到堆中,直到所有结点添加完毕。
struct cmp {
bool operator()(ListNode * x, ListNode * y) {
return x->val > y->val;
}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode*, vector<ListNode*>, cmp> q;
for(int i = 0; i < lists.size(); i++)
if(lists[i] != NULL)
q.push(lists[i]);
ListNode *dummy = new ListNode(0);
ListNode *cur = dummy, *tmp = NULL;
while(!q.empty()) {
tmp = q.top();
q.pop();
cur->next = tmp;
cur = cur->next;
if(tmp->next != NULL)
q.push(tmp->next);
}
cur = dummy->next;
delete dummy;
return cur;
}
};
思路三(以下方法较差):直接暴力获得所有结点值,调用sort后直接构造新链表,我觉得应该是leetcode的数据太水,才能有这么高的效率。
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *>& lists) {
std::vector<int> vals;
for (ListNode *l : lists) {
ListNode *it = l;
while (it) {
vals.push_back(it->val);
it = it->next;
}
}
std::sort(vals.begin(), vals.end());
ListNode *sorted_ll = nullptr;
ListNode *it = sorted_ll;
for (int val : vals)
{
ListNode *new_node = new ListNode(val);
// first node in ll
if (!sorted_ll)
sorted_ll = new_node;
else
it->next = new_node;
it = new_node;
}
return sorted_ll;
}
};思路四:每次轮流查找最小值加入到新的链表中,效率最低。
class Solution {
public:
bool check(vector<ListNode*>& lists) {
for(int i = 0; i < lists.size(); i++)
if(lists[i] != NULL)
return true;
return false;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
int index = 0, cur_val = INT_MAX;
ListNode * res = new ListNode(0);
ListNode * tmp = res;
while(check(lists)) {
cur_val = INT_MAX;
for(int i = 0; i < lists.size(); i++) {
if(lists[i] != NULL && lists[i]->val < cur_val) {
cur_val = lists[i]->val;
index = i;
}
}
tmp->next = lists[index];
lists[index] = lists[index]->next;
tmp = tmp->next;
}
tmp = res;
res = res->next;
return res;
}
};思路五:用最小堆进行优化,代替每次查询的过程,但是自己写的最小堆效率略差。
vector<ListNode *> heap;
void shiftUp(int end) {
int son = end;
while(son > 0) {
int parent = son % 2 == 0 ? son/2-1 : son/2;
if(heap[son]->val < heap[parent]->val) {
swap(heap[son], heap[parent]);
son = parent;
}
else
son--;
}
}
void shiftDown(int start, int end) {
int parent = start;
while (parent < end) {
int lchild = parent*2+1, rchild = parent*2+2;
if ((lchild <= end && heap[parent]->val > heap[lchild]->val) ||
(rchild <= end && heap[parent]->val > heap[rchild]->val)) {
if (heap[lchild]->val < heap[rchild]->val) {
swap(heap[parent], heap[lchild]);
parent = lchild;
}
else {
swap(heap[parent], heap[rchild]);
parent = rchild;;
}
}
else
parent++;
}
}
void insert(ListNode * node) {
heap.push_back(node);
shiftUp(heap.size()-1);
}
ListNode * removeMin() {
if(heap.size() == 0) return NULL;
ListNode * tmp = heap[0];
heap[0] = heap[heap.size()-1];
heap.pop_back();
shiftDown(0, heap.size()-1);
return tmp;
}
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
for(int i = 0; i < lists.size(); i++)
if(lists[i] != NULL)
insert(lists[i]);
ListNode * res = new ListNode(0);
ListNode * cur = res;
ListNode * tmp = removeMin();
while(tmp != NULL) {
cur->next = tmp;
cur = cur->next;
if(tmp->next != NULL)
insert(tmp->next);
tmp = removeMin();
}
tmp = res;
res = res->next;
delete tmp;
return res;
}
};
1863




被折叠的 条评论
为什么被折叠?



