【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
题意:把k个排序成一个有序链表。
用优先队列先把k个链表遍历一遍把值存起来,在建一个新链表吧数从优先队列里一个个放进去,注意空指针的判断。
/**
* 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 i,j,len=lists.size();
priority_queue<int,vector<int>,greater<int>> q;
int flag=1;
while(flag!=0)
{
flag=0;
for(i=0;i<len;++i)
{
if(lists[i])
{
flag=1;
q.push(lists[i]->val);
lists[i]=lists[i]->next;
}
}
}
if(q.empty())return NULL;
ListNode *root,*now;
root=new ListNode(q.top());
root->next=NULL;
now=root;
q.pop();
while(!q.empty())
{
now->next=new ListNode(q.top());
now=now->next;
q.pop();
now->next=NULL;
}
return root;
}
};
【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)的更多相关文章
- [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 ...
- LeetCode: Merge Two Sorted Lists 解题报告
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
- 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 ...
- [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 ...
- leetcode Merge Two Sorted Lists python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Merge Two Sorted Lists 归并排序
题意: 将两个有序的链表归并为一个有序的链表. 思路: 设合并后的链表为head,现每次要往head中加入一个元素,该元素要么属于L1,要么属于L2,可想而知,此元素只能是L1或者L2的首个元素, ...
- LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)
21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...
- [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 ...
随机推荐
- 从不同层面看cocos2d-x
一 框架层面 二 Lua层面 三 工具层面 四 android打包 一 框架层 总体来说,cocos2dX提供的一个简便的框架,包括了渲染,动画,事件分发,网络还有UI,物理引擎等几大 ...
- Hadoop基本概念
一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运算和存储.Hadoop实现了一个分布式文件系统(Hadoop Dist ...
- 一、Solr综述
什么是Solr搜索 我们经常会用到搜索功能,所以也比较熟悉,这里就简单的介绍一下搜索的原理. 当然只是介绍solr的原理,并不是搜索引擎的原理,那会更复杂. 流程图 这是一个非常简单的流程图: Use ...
- cocos2d-x anchorPoint
之前一直没有用过anchorPoint,也感觉用这个东西的地方相对比较少的,都是直接使用世界坐标来定位的. 但是,在现在这个项目中,却有同事使用了这个anchorPoint,使用是使用了,但是,在碰撞 ...
- Ubuntu 搭建NDK环境
一. NDK下载地址 https://developer.android.com/tools/sdk/ndk/index.html 二. NDK环境两种方式 NDK下载后,解压缩后放置于目录/home ...
- 编译lua5.3.2报错提示libreadline.so存在未定义的引用解决方法
从官网上下载5.3.2的源码后,make linux进行编译,提示报错: gcc -std=gnu99 -o lua lua.o liblua.a -lm -Wl,-E -ldl -lreadline ...
- windows身份验证,那么sqlserver的连接字符串的
Data Source=计算机名称或ip地址;Initial Catalog=数据库名称;Integrated Security=True windows身份验证不需要psw的Provider=SQL ...
- iOS字体 UIFont 字体名字大全
打印所有的字体 NSArray *familyNames = [UIFont familyNames];//所有的家族名称 for(NSString *familyName in familyName ...
- NOIP201504推销员
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...
- 使用inline-block做水平垂直居中
父级宽高不定,如何使子元素水平垂直居中? 下面是用 display: inline-block 实现的: <!doctype html> <html lang="en&qu ...