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 ...
随机推荐
- Objective-C 中如何测量代码的效率
背景 在我们编程的时候,可能经常会有一些疑问: * 我们写的某个方法的执行效率是多少? * 方法 A 和 方法 B 哪个更快? 因此,我们不可避免的要用到一些方法来计算代码的执行效率.计算代码的执行效 ...
- Linux 高性能服务器编程——Linux服务器程序规范
问题聚焦: 除了网络通信外,服务器程序通常还必须考虑许多其他细节问题,这些细节问题涉及面逛且零碎,而且基本上是模板式的,所以称之为服务器程序规范. 工欲善其事,必先利其器,这篇主要来探 ...
- Servlet - 基础
Servlet 标签 : Java与Web HTTP协议 HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器(Browser)和万维网服务 ...
- 内存数据网格IMDG简介
1 简介 将内存作为首要存储介质不是什么新鲜事儿,我们身边有很多主存数据库(IMDB或MMDB)的例子.在对主存的使用上,内存数据网格(In Memory Data Grid,IMDG)与IMDB类似 ...
- Linux--FTP和MAIL服务器
1) FTP协议 FTP(FileTransfer Protocol,文件传输协议)用于管理计算机之间的文件传送.FTP 是Internet 上使用非常广泛的一种通讯协议,它是由支持Intern ...
- iOS注册远程推送消息证书后提示此证书签发者无效的解决办法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们在编写关于远程推送消息的App时需要注册一个相关的证书,我 ...
- 15 ActionBar.Tab 以及保存fragment对象 代码案例
API 21弃用 values 中 string文件源码: <?xml version="1.0" encoding="utf-8"?> <r ...
- 6.0、Android Studio性能优化工具
显示图像包含四个步骤.简单来说,CPU对比显示列表,GPU渲染图片显示,内存存储图片和数据,电池提供点力能源.每个部分的硬件都有限制,超过这个限制会导致应用运行较慢,显示性能差,或者耗电. 为了查找造 ...
- [ExtJS5学习笔记]第二十三节 Extjs5中表格gridpanel的列格式设置
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39665979 官方文档:http://docs.sencha.com/extjs/5. ...
- UNIX网络编程——使用select函数的TCP和UDP回射服务器程序
服务器程序: #include <sys/wait.h> #include <string.h> #include <string.h> #include < ...