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 ...
随机推荐
- spark[源码]-sparkContext详解[一]
spark简述 sparkContext在Spark应用程序的执行过程中起着主导作用,它负责与程序和spark集群进行交互,包括申请集群资源.创建RDD.accumulators及广播变量等.spar ...
- python中的引用传递,可变对象,不可变对象,list注意点
python中的引用传递 首先必须理解的是,python中一切的传递都是引用(地址),无论是赋值还是函数调用,不存在值传递. 可变对象和不可变对象 python变量保存的是对象的引用,这个引用指向堆内 ...
- lightGallery 一个视屏不播放 解决方法
这次使用了lightGallery,感觉还不错.样式比较美观,并且支持响应式. 使用过程中,我遇到了下面的问题: 当我 .picsgallery里面只有一个 .gItem的时候.点击弹出幻灯片,再点击 ...
- 在eclipse中new 对象后怎么通过快捷键自动生成返回对象
如题,每次new 对象的时候不想手动补全返回对象,可以实现快捷键生成返回对象.new 对象后可以按住ctrl+1,如下图: 选择第一行即可.
- Selenium+Python常见定位方法
参见官网:http://selenium-python.readthedocs.io/locating-elements.html 有多种策略来定位页面中的元素.你可以使用最适合你的情况.Seleni ...
- [原创]css中a标签去掉锚点文本下划线
我对博客的认识是:记录问题,解决问题,分享知识.如果有轮子,我不需要造轮子. 1.问题解决方式: 设置属性:text-decoration:none; 2.更多属性参数参考 text-decorati ...
- Python3.x:遍历select下拉框获取value值
Python3.x:遍历select下拉框获取value值 Select提供了三种选择方法: # 通过选项的顺序,第一个为 0 select_by_index(index) # 通过value属性 s ...
- tomcat+svn+maven+jenkins实现自动构建
首先说明一个各软件的版本: tomcat:apache-tomcat-8.5.16.tar.gz maven:apache-maven-3.5.0-bin.tar.gz svn:subversion- ...
- 20145333 《Java程序设计》第5周学习总结
20145333 <Java程序设计>第5周学习总结 教材学习内容总结 语法与继承架构 使用try.catch Java中所有错误都会被包装成对象,可以尝试(try)执行程序并捕捉(cat ...
- 【纯代码】Swift-自定义PickerView单选(可修改分割线颜色、修改字体大小、修改字体颜色。)(可根据需要自己扩展)
typealias PopPickerViewCallBackClosure = (_ resultStr:NSString?) -> () class PopPickerView : UIVi ...