LeetCode OJ:Merge k Sorted Lists(归并k个链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
类似于归并2个链表,暴力一点的方法就是,每取出一个list就与以前的list归并返回merge后list,知道所有list merge完成。
但是可惜,这样做会TLE。贴下代码先:
/**
* 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) {
ListNode * ret = NULL;
for(auto * it : lists){
ret = mergeList(ret, it);
}
return ret;
} ListNode * mergeList(ListNode * head1, ListNode * head2)
{
if(!head1) return head2;
if(!head2) return head1;
ListNode * head = new ListNode(-);
head->next = head1;
ListNode * prev = head;
ListNode * tmpNode = NULL;
while(head1 && head2){
if(head1->val < head2->val){
prev = prev->next;
head1 = head1->next;
}else{
tmpNode = head2->next;
prev->next = head2;
head2->next = head1;
prev = head2;
head2 = tmpNode;
}
}
if(head2){
prev->next = head2;
}
return head->next;
}
};
下面是用堆来做的,先建立一个小堆,找到做小元素。将其merge到一个链表里面。如果后面还有元素,再将其放到堆中。代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
static bool Comp(ListNode * l1, ListNode * l2)
{
if(l1->val < l2->val)
return false;
return true;
} ListNode* mergeKLists(vector<ListNode*>& lists) {
vector<ListNode *> removeNullList;
removeNullList.reserve(lists.size());
for(auto it = lists.begin(); it != lists.end(); ++it){
if(*it != NULL)
removeNullList.push_back(*it);
}
if(removeNullList.size() == ) return NULL; //首先去除Null节点
make_heap(removeNullList.begin(), removeNullList.end(), Comp);
ListNode * helper = new ListNode(-);
ListNode * tail = helper;
ListNode * minNode = NULL;
while(!removeNullList.empty()){
pop_heap(removeNullList.begin(), removeNullList.end(), Comp);
minNode = removeNullList[removeNullList.size()-];
removeNullList.pop_back();
tail->next = minNode;
tail = tail->next;
if(minNode->next){//如果后面还有list的话,再次入堆
removeNullList.push_back(minNode->next);
make_heap(removeNullList.begin(), removeNullList.end(), Comp);
}
}
return helper->next;
}
};
LeetCode OJ:Merge k Sorted Lists(归并k个链表)的更多相关文章
- [LeetCode] 21. Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [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]21. Merge Two Sorted Lists合并两个链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [LeetCode] 21. Merge Two Sorted Lists 合并有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [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 Two Sorted Lists Merge K Sorted Lists
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
- 【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] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- sparkSQL整体实现框架
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://9269309.blog.51cto.com/9259309/1845525 这篇 ...
- C++ 单链表的实现
#ifndef LINKEDLIST_H #define LINKEDLIST_H #define MAXLEN 256 template <class T> struct LinkedL ...
- linux 登录数据库
-h 地址 (例如:127.0.0.1,localhost) mysql -h 127.0.0.1 -u root -p
- DDOS防护原理
1.常见DDoS攻击分类 DDoS粗略分类为流量型攻击和CC攻击.流量型攻击主要是通过发送报文侵占正常业务带宽,甚至堵塞整个数据中心的出口,导致正常用户访问无法达到业务服务器.CC攻击主要是针对某些业 ...
- 20145216史婧瑶《Java程序设计》第2周学习总结
20145216 <Java程序设计>第2周学习总结 教材学习内容总结 第三章 基础语法 3.1 类型.变量与运算符 •类型 •基本类型 •整数:short(占2字节).int(占4字节) ...
- 20144303石宇森 《Java程序设计》第2周学习总结
---恢复内容开始--- 20144303 <Java程序设计>第2周学习总结 教材学习内容总结 一.类型: 1.Java可以区分为基本类型和类类型.类类型也称作参考类型. 2.Java中 ...
- 20145211《网络渗透》MS12-004漏洞渗透
20145211<网络渗透>MS12-004漏洞渗透 一 实验原理 初步掌握平台matesploit的使用 有了初步完成渗透操作的思路 在这里我选择对的不是老师推荐的MS11_050,而是 ...
- minSdk(API 21) > deviceSdk(API 17)解决
运行一个开源的项目出现“minSdk(API 21) > deviceSdk(API 17)”的提示,因为我用的是手机是sdk(API17)的,而项目要求是最低版本是minSdk(API 21) ...
- mybatis的一级缓存和二级缓存(1)
1.mybatis一级缓存,sqlSesion级别的缓存,一级缓存默认一直开启的,sqlSession级别的一个Map,把查询的数据放到一个Map中,以后需要相同的数据,直接从Map中去取 与数据库一 ...
- docker 集群 笔记
docker 集群 Docker 容器 移除所有的容器和镜像(大扫除) 用一行命令大扫除: docker kill $(docker ps -q) ; docker rm $(docker ps -a ...