【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 ...
随机推荐
- [React Testing] Setting up dependencies && Running tests
To write tests for our React code, we need to first install some libraries for running tests and wri ...
- Java基础知识强化42:StringBuffer类之StringBuffer的截取功能
1. StringBuffer的截取功能: public String subString(int Start): public String subString(int Start, int end ...
- HID Boot device.
整理这篇文章的目的: 客户会有用到遥控器部分(遥控器操作flow:当按下某个键时,MCU会透过UR送command给TP,TP吃到后再透过微软标准的keyboard上报) 要求:在BIOS设定阶段,遥 ...
- 常用分组函数count-avg-sum-max-min
分组函数也称多行函数,用于对一组数据进行运算,针对一组数据(取自于多行记录的相同字段)只返回一个结果,例如计算公司全体员工的工资总和.最高工资.最低工资.各部门的员工平均工资(按部门分组)等.由于分组 ...
- UITextField点击return后注销第一响应者
// 当点击了return按钮,就让text调用自己的endEditing方法 [textField addTarget:textField action:@selector(endEditing:) ...
- ORA-25153: Temporary Tablespace is Empty解决方法
SQL> @/tmp/4.txt create table huang_1 (deptno number,dname varchar2(19),loc varchar2(20)) * ERROR ...
- java学习笔记(4):内存管理
在内存的方面,Java自身带有垃圾运行机制,表面上好像我们可以忽略不计,但是如果我们对其加以理解的话,说不定有时会达到事半功倍的效果. 所以自己花些时间整理一些此类的资料. 一.基本概念 1.栈 对于 ...
- hdu 5188
it's a dp difficult problem 试想如果我们遇见这样一道题,: 有n道题目,每道题有一个得分v和用时t: 我们要得够w分:用时最少 怎么做?? 这是一个裸奔的01背包 如 ...
- Jquery 网站保存信息提示消息实现,提示后自动消失
现在的大多数网站都有校验,以及信息提示:为了给用户更好的体验可以不用alert提示消息,采用jQuery提示完消息,自动消失 css <style> #tip_message { z-in ...
- android 巧用finish方法
在android应用开发中,我们从一个activity跳到另一个activity时,我么要用到Intent: eg:Intent intent = new Intent(A.this,B.class) ...