1. Merge Two Sorted Lists

  题目链接

  题目要求:

   Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

  这道题目题意是要将两个有序的链表合并为一个有序链表。为了编程方便,在程序中引入dummy节点。具体程序如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode();
ListNode *head = nullptr, *start = dummy;
int flag = true;
while(l1 && l2)
{
if(l1->val < l2->val)
{
start->next = l1;
l1 = l1->next;
}
else
{
start->next = l2;
l2 = l2->next;
}
start = start->next;
} if(!l1)
start->next = l2;
else if(!l2)
start->next = l1; head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};

  2. Merge k Sorted Lists

   题目链接

  题目要求:

  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) {
int sz = lists.size();
if(sz == )
return nullptr; ListNode *node = new ListNode();
ListNode *head = nullptr, *start = node;
while(true)
{
int count = ;
int minVal = INT_MAX;
int minNode = -;
for(int i = ; i < sz; i++)
{
if(lists[i] && lists[i]->val < minVal)
{
minNode = i;
minVal = lists[i]->val;
}
else if(!lists[i])
count++;
} if(count != sz)
{
start->next = lists[minNode];
lists[minNode] = lists[minNode]->next;
}
else
{
head = node->next;
delete node;
node = nullptr; return head;
} start = start->next;
}
}
};

  后来参考网上的做法,用Divide and Conquer方法会比较好,具体程序如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *node = new ListNode();
ListNode *head = nullptr, *start = node;
int flag = true;
while(l1 && l2)
{
if(l1->val < l2->val)
{
start->next = l1;
l1 = l1->next;
}
else
{
start->next = l2;
l2 = l2->next;
}
start = start->next;
} if(!l1)
start->next = l2;
else if(!l2)
start->next = l1; head = node->next;
delete node;
node = nullptr; return head;
} ListNode *mergeKLists(vector<ListNode*>& lists, int low, int high)
{
if(low < high)
{
int mid = (low + high) / ;
return mergeTwoLists(mergeKLists(lists, low, mid), mergeKLists(lists, mid + , high));
}
return lists[low];
} ListNode* mergeKLists(vector<ListNode*>& lists) {
int sz = lists.size();
if(sz == )
return nullptr; return mergeKLists(lists, , sz - );
}
};

  算法分析摘自同一博文

  我们来分析一下上述算法的时间复杂度。假设总共有k个list,每个list的最大长度是n,那么运行时间满足递推式T(k) = 2T(k/2)+O(n*k)。根据主定理,可以算出算法的总复杂度是O(nklogk)。如果不了解主定理的朋友,可以参见主定理-维基百科。空间复杂度的话是递归栈的大小O(logk)。

LeetCode之“链表”:Merge Two Sorted Lists && Merge k Sorted Lists的更多相关文章

  1. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  2. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  3. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  4. 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)

    题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  5. [leetcode]23. Merge k Sorted Lists归并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. I ...

  6. 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  7. LeetCode Merge k Sorted Lists (链表)

    题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  8. LeetCode OJ:Merge k Sorted Lists(归并k个链表)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 类 ...

  9. [LeetCode]23. Merge k Sorted Lists合并K个排序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

随机推荐

  1. MacOS获取辅助功能权限控制鼠标点击事件

    昨晚玩一个模拟经营的游戏,由于升级太慢我就不停的种树卖树来换取经验值.不过重复点击10几分钟后,实在受不了.网上本来准备找个鼠标自动点击的软件用用.结果没找到趁手的.如是自己写了个. 自己设置需要点击 ...

  2. springMVC源码分析--HandlerMethod

    在之前的博客中我们已经接触过HandlerMethod,接下来我们简单介绍一下HandlerMethod,简单来说HandlerMethod包含的信息包括类.方法和参数的一个信息类,通过其两个构造函数 ...

  3. “出错了”和报告Bug的艺术

    "出错了." 没有那句话能像"出错了"一样让程序员/开发者如此沮丧,心里翻江倒海,怒火一点即燃,还要死掉一大片脑细胞. 这句生硬的开场白通常标志着让开发者恐惧的 ...

  4. 安卓Button-TextView-EditText综合运用

    1.如何使用安卓中的按键Button? 1.先从控件库拖一个按钮button的控件,在XML设置好宽高等参数 对应的就是Button这个图标,直接拖出来即可; 以下是设置这个按钮对应的XML代码: & ...

  5. Android Multimedia框架总结(六)C++中MediaPlayer的C/S架构

    转载请把头部出处链接和尾部二维码一起转载,本文出自: http://blog.csdn.net/hejjunlin/article/details/52435789 前面几节中,都是通过java层调用 ...

  6. Android 系统自动重启Bug(高通平台)

    点击打开链接 最近客户反馈了一个Bug,我们的系统用着用着会自动重启,尤其是在拨号的时候极容易死机或者进入下载模式.根据老大和高通的支持得到了一个解决方案. 在Android系统中,有这么一个文件夹: ...

  7. 05 Activity 回传数据

    当从一个Activity跳转到第二个Activity后然 让其处理完业务逻辑回传数据给第一个Activity: 回传调用方法顺序: onActivityResult--->>onResta ...

  8. Mahout学习路线图

    转自:http://blog.fens.me/hadoop-mahout-roadmap/ Mahout学习路线图 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, ...

  9. UNIX网络编程——TCP—经受时延与nagle算法、滑动窗口、拥塞窗口

    1.经受时延: TCP在接收到数据时并不立即发送ACK,相反,它推迟发送,以便将ACK与需要沿该方向发送的数据一起发送,时延为200ms,超过时延范围,发送确认. 2.nagle算法: 一个TCP连接 ...

  10. 后端分布式系列:分布式存储-HDFS 异常处理与恢复

    在前面的文章 <HDFS DataNode 设计实现解析>中我们对文件操作进行了描述,但并未展开讲述其中涉及的异常错误处理与恢复机制.本文将深入探讨 HDFS 文件操作涉及的错误处理与恢复 ...