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 ...
随机推荐
- 自定义圆形头像CircleImageView的使用和源码分析
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0806/3268.html tools:context="com.ex ...
- TypeScript 3.3来了!快看看有什么新功能
翻译:疯狂的技术宅原文:https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript 本文首发微信公众号:jingchen ...
- lamp编译详解
首先确认系统环境:centos6.4 min版本 1.安装需要的开发环境 yum groupinstall "Development Tools" "Server Pla ...
- SVN主从高可用
https://blog.csdn.net/conwayli/article/details/80235738
- 【Network Architecture】Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning(转)
文章来源: https://www.cnblogs.com/shouhuxianjian/p/7786760.html Feature Extractor[Inception v4] 0. 背景 随着 ...
- Glibc-2.3.4编译
$tar xf Glibc2.3.4.tar.bz2 $mkdir build_glibc $cd build_glibc ../glibc-2.3.4/configure --prefix=/too ...
- codevs 1085 数字游戏 dp或者暴搜
1085 数字游戏 2003年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单 ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/tmp/mysql.sock'
查看是否开启服务 # ps -ef | grep mysql root 5605 5457 0 11:45 pts/2 00:00:00 grep mysql 查看my.cnf # cat /etc/ ...
- Memcached delete 命令
Memcached delete 命令用于删除已存在的 key(键). 语法: delete 命令的基本语法格式如下: delete key [noreply] 多个 key 使用空格隔开,如下: d ...
- C++学习——C++复合类型
1.引用 引用是为某一个变量起了另一个名字,定义方式为type &rval = val; 引用类型必须与引用的变量类型完全一致,引用后,rval和val将会被视为一个变量,只不过有两种调用方式 ...