题目: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个链表)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. leetcode Merge Two Sorted Lists python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  6. LeetCode Merge Two Sorted Lists 归并排序

      题意: 将两个有序的链表归并为一个有序的链表. 思路: 设合并后的链表为head,现每次要往head中加入一个元素,该元素要么属于L1,要么属于L2,可想而知,此元素只能是L1或者L2的首个元素, ...

  7. LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

    21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...

  8. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  9. [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 ...

随机推荐

  1. 台式电脑部署xen虚拟化的各种问题

    本打算用一台台式机做xen虚拟化,搞了一天搞得焦头烂额还是没搞定,中间遇到各种奇葩问题,这里mark一下 1.计划用三块2TB的SATA硬盘,然后装centos5 做虚拟化,结果忘记了centos5最 ...

  2. mybatis知识总结

    基于昨天的mybatis入门详解,今天我们再来看看mybatis稍微高深些的知识点. 1.解决Model属性和数据库字段不一致的问题 1),开启驼峰命名 2),使用resultMap进行映射, < ...

  3. tomcat程序记录客户端真实IP

    需求: 开发告知:让后端tomcat日志获取真实的IP,而不是nginx 服务器的IP tomcat前面是nginx做的反向代理,所以tomcat取到的是nginx的ip. 日志名称是localhos ...

  4. 整理:C#写ActiveX, 从代码到打包到签名到发布的示例

    对于不懂C++和VB的我, 在工作上却遇到需要重写旧ActiveX控件的任务. 好在客户机都是Windows PC, 基本上都有.net framework 2.0, 勉强用C#实现可以满足需求 所以 ...

  5. SQL流程控制语句学习(三):while break continue

    1.while语法 while   布尔表达式 {sql语句或语句块} break  --跳出本层循环 {sql语句或语句块} continue  --跳出本次循环 {sql语句或语句块} 2.whi ...

  6. JS 无提示关闭当前窗口

    function teseClose() { window.opener = null; window.open('','_self'); window.close(); }

  7. Dijkstra算法 最短路径 (部分)

    void Dijkstra(int n, int v, int *dist, int *prev, int c[maxnum][maxnum]) {     bool s[maxnum];       ...

  8. Sicily 1323. Switch text

    题目地址:1323. Switch text 思路: 题目意思不好理解呀. 题目意思是这样的:输入两个测试数据,首先,两个测试数据本身得各自前后倒转,然后两个测试数据倒转后的结果再各自对半互换,然后测 ...

  9. polygonZM---> poliygon

    ArcToolbox > Conversion Tools > To Shapefile > Feature Class To Shapefile (multiple)   Clic ...

  10. Intellij idea配置springMvc4.2.6

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. 环境: Intellij iead 2016.1 java version " ...