您的位置:首页 > 其它

leetcode 023 —— Merge k sorted linked lists

2015-07-13 14:24 363 查看
Merge k sorted
linked lists and return it as one sorted list. Analyze and describe its complexity.

思路,采用小顶堆

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
vector<ListNode*>::iterator it = lists.begin();
while(it != lists.end()) {
if(*it == NULL) lists.erase(it);
else ++it;
}
if(lists.size() < 1) return NULL;

ListNode *head = NULL, *cur = NULL;
make_heap(lists.begin(), lists.end(), comp());

while(lists.size() > 0) {
if(head == NULL) head = cur = lists[0];
else cur = cur->next = lists[0];

pop_heap(lists.begin(), lists.end(), comp());
int last = lists.size() - 1;
if(lists[last]->next != NULL) {
lists[last] = lists[last]->next;
push_heap(lists.begin(), lists.end(), comp());
}
else lists.pop_back();
}

return head;
}
class comp {
public:
bool operator() (const ListNode* l, const ListNode* r) const {
return (l->val > r->val);
}
};

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: