【Merge K Sorted Lists】cpp

题目:

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) {
        if (lists.size()==0) return NULL;
        return Solution::mergeK(lists, 0, lists.size()-1);
    }
    static ListNode* mergeK(vector<ListNode*>& lists, int begin, int end )
    {
            if ( begin==end ) return lists[begin];
            if ( (begin+1)==end ) return Solution::mergeTwo(lists[begin], lists[end]);
            int mid = ( begin + end ) / 2;
            ListNode *firstHalf = Solution::mergeK(lists, begin, mid);
            ListNode *secondHalf = Solution::mergeK(lists, mid+1, end);
            return Solution::mergeTwo(firstHalf, secondHalf);
    }
    static ListNode* mergeTwo( ListNode *h1, ListNode *h2 )
    {
            ListNode dummy(-1);
            ListNode *p = &dummy;
            while ( h1 && h2 ){
                if ( h1->val<h2->val ){
                    p->next = h1;
                    h1 = h1->next;
                }
                else{
                    p->next = h2;
                    h2 = h2->next;
                }
                p = p->next;
            }
            p->next = h1 ? h1 : h2;
            return dummy.next;
    }
};
复制代码

tips:

多路归并写法。

=================================================

第二次过这道题,第一次没有写成归并的形式,结果超时了。

在网上查了一下这道题的时间复杂度分析:http://www.cnblogs.com/TenosDoIt/p/3673188.html

改成了归并的写法,AC了。

复制代码
/**
 * 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)
        {
            return Solution::divide(lists, 0, lists.size()-1);
        }
        ListNode* divide(vector<ListNode*>& lists, int begin, int end)
        {
            if ( begin>end ) return NULL;
            if ( begin==end ) return lists[begin];
            int mid = (begin+end)/2;
            ListNode* l = Solution::divide(lists, begin, mid);
            ListNode* r = Solution::divide(lists, mid+1, end);
            return Solution::merge2Lists(l, r);
        }
        static ListNode* merge2Lists(ListNode* p1, ListNode* p2)
        {
            ListNode head(0);
            ListNode* p = &head;
            while ( p1 && p2 )
            {
                if ( p1->val < p2->val )
                {
                    p->next = p1;
                    p1 = p1->next;
                }
                else
                {
                    p->next = p2;
                    p2 = p2->next;
                }
                p = p->next;
            }
            p->next = p1 ? p1 : p2;
            return head.next;
        }
};
复制代码

 用非递归又写了一个。

复制代码
/**
 * 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)
        {
            if ( lists.empty() ) return NULL; 
            int length = lists.size();
            while ( length>1 )
            {
                int index = 0;
                int i=1;
                for ( ; i<length; i=i+2)
                {
                    lists[index++] = Solution::merge2Lists(lists[i], lists[i-1]);
                }
                if ( length & 1 ) lists[index++] = lists[i-1]; 
                length = index;
            }
            return lists[0];
        }
        static ListNode* merge2Lists(ListNode* p1, ListNode* p2)
        {
            ListNode head(0);
            ListNode* p = &head;
            while ( p1 && p2 )
            {
                if ( p1->val < p2->val )
                {
                    p->next = p1;
                    p1 = p1->next;
                }
                else
                {
                    p->next = p2;
                    p2 = p2->next;
                }
                p = p->next;
            }
            p->next = p1 ? p1 : p2;
            return head.next;
        }
};
复制代码

=====================================================

之前两次过这道题,即使看了上面提到blog的解释,也没太直观理解为什么归并的效率高。

第三次过,有点儿顿悟了:其实就是插入排序和归并排序的效率区别;只不过这次归并的是链表

 

posted on   承续缘  阅读(168)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示