LeetCode之“链表”:Merge Two Sorted Lists && Merge k Sorted Lists
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的更多相关文章
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [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 ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- [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 ...
- 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- LeetCode Merge k Sorted Lists (链表)
题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode OJ:Merge k Sorted Lists(归并k个链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 类 ...
- [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 ...
随机推荐
- Android开发学习之路--逆向分析反编译
一般情况下我们想要了解别人的app怎么实现这个动画,这个效果的时候,总是会想到反编译一下,看下布局,看下代码实现.对,这对于有经验的玩家确实手到擒来了,但是初学者,根本就不知道怎么反编译,怎么看代 ...
- how to output quotes in bash prompt
introduction In certain situations, quotes are required to be output in the command prompt. To do th ...
- Appium移动自动化框架初探
作者:cryanimal QQ:164166060 本文简要介绍了appnium自动化框架的架构.加载流程.支持语言.相关配置,以及元素定位工具等. 官方网站: http://appium.io Ap ...
- weakref 待解决.
暂时不知为何在控制台多执行一次b()后,del a就不会立即销毁Foo实例. >>> class Foo(object): def __init__(self): self.obj ...
- Dynamics CRM2016 Web API之创建记录
前篇介绍了通过primary key来查询记录,那query的知识点里面还有很多需要学习的,这个有待后面挖掘,本篇来简单介绍下用web api的创建记录. 直接上代码,这里的entity的属性我列了几 ...
- 物料REVISION控制
--新增 INV_ITEM_REVISION_PUB.Create_Item_Revision ( p_api_version IN NUMBER , p_init_msg_list IN VARCH ...
- android解析网络json数据(1)
1.首先获得url,传入URL类,利用URL的openconnection方法,获得URLConnection,去的输入流,进行操作,具体代码如下: public class NetConnectio ...
- 安卓程序员要拿到5000和1w的薪资,分别需要掌握哪些技术?
这个是我在逛知乎的时候发现的一个帖子,在这里小小的整理了一下,收集了一些评论,然后我分享出来,希望对自己还有同行有所帮助. 著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 链接 ...
- 【NPR】卡通渲染
写在前面 我的博客讲过好几篇卡通渲染了,比如[Unity Shader实战]卡通风格的Shader(一).[Unity Shader实战]卡通风格的Shader(二).[NPR]漫谈轮廓线的渲染.[S ...
- iOS下JS与OC互相调用(一)--UIWebView 拦截URL
最近准备把之前用UIWebView实现的JS与原生相互调用功能,用WKWebView来替换.顺便搜索整理了一下JS 与OC 交互的方式,非常之多啊.目前我已知的JS 与 OC 交互的处理方式: * 1 ...