Merge k Sorted Lists

方法一,将第一个list一次和后面的所有list合并,每次合并调用Merge Two Sorted List方法。假设每个List长度为n,Time Complexity O(n*k^2)。空间O(1)。代码如下:

class Solution {
public:
    ListNode *mergeKLists(vector<ListNode *> &lists) {
        if(lists.size() == 0) return NULL;
        ListNode *fir = lists[0];
        for(int i=1; i<lists.size(); i++)
        {
            fir = mergeTwoList(fir, lists[i]);
        }
        return fir;
    }
    ListNode *mergeTwoList(ListNode *l1, ListNode *l2)
    {
        if(l1 == NULL) return l2;
        if(l2 == NULL) return l1;
        ListNode head(-1);
        for(ListNode *p = &head; l1 != NULL || l2 != NULL; p = p->next)
        {
            int val1 = l1? l1->val: INT_MAX;
            int val2 = l2? l2->val: INT_MAX;
            if(val1 < val2)
            {
                p->next = l1;
                l1 = l1->next;
            }
            else
            {
                p->next = l2;
                l2 = l2->next;
            }
        }
        return head.next;
    }
};
方法二,对每个List的第一个元素使用heap_sort建立一个最小堆。获取堆顶的元素作为merged list中的下一个元素,然后将该元素从heap中pop出来,并push该元素在原始list中的下一个元素,继续sort,并取下一个元素,直到所有的list均为空。时间复杂度O(nklogk),空间O(k)。代码如下:

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;
            
        make_heap(lists.begin(), lists.end(), comp());
        ListNode *head = NULL, *cur = NULL;
        
        while(lists.size()>0)
        {
            if(head == NULL)
                head = cur = lists[0];
            else
            {
                cur->next = lists[0];
                cur = cur->next;
            }
            pop_heap(lists.begin(), lists.end(), comp());
            
            int last = lists.size()-1;
            if(lists[last]->next == NULL)
                lists.pop_back();
            else
            {
                lists[last] = lists[last]->next;
                push_heap(lists.begin(), lists.end(), comp());
            }
        }
        return head;
    }
    
    class comp
    {
        public:
        bool operator()(const ListNode *l1, const ListNode *l2)
        {
            if(l1->val > l2->val)
                return 1;
            else
                return 0;
        }
    };
};


Method 2 using STL priority_queue。

class comp
{
public:
    bool operator()(const ListNode *lhs, const ListNode *rhs)
    {
        return lhs->val > rhs->val;
    }
};
    
class Solution {
public:
    ListNode *mergeKLists(vector<ListNode *> &lists) {
        priority_queue<ListNode*, vector<ListNode*>, comp> queue;
        
        ListNode dummy(-1);
        ListNode *cur = &dummy;
        
        for(auto list: lists)
        {
            if(list) queue.push(list);
        }
        
        while(!queue.empty())
        {
            ListNode *top = queue.top();
            queue.pop();
            cur->next = top;
            cur = cur->next;
            if(top->next) queue.push(top->next);
        }
        return dummy.next;
    }
};

参考:http://blog.csdn.net/linhuanmars/article/details/19899259

http://discuss.leetcode.com/questions/204/merge-k-sorted-lists

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值