题目: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. 宙斯是一个完整的Hadoop的作业平台[转]

    https://github.com/alibaba/zeus 宙斯(zeus)是什么 宙斯是一个完整的Hadoop的作业平台从Hadoop任务的调试运行到生产任务的周期调度 宙斯支持任务的整个生命周 ...

  2. 一些使用Android设备调试功能的注意事项(挖职位)

    华为3C Activity切换动画过热. 当显示器是不是大图easy显现OOM(应用最大大于其他手机内容).因此,调试OOM不要当问题用这个手机,否则,很难发现问题. 小米3 不要调用系统的裁图功能. ...

  3. 通过layout实现可拖拽自动排序的UICollectionView

    文/CenturyGuo(简书作者)原文链接:http://www.jianshu.com/p/8d1bf1838882著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. Translat ...

  4. CVE-2015-8660分析

    0x00测试环境   使用环境 备注 操作系统 Ubuntu15.04 虚拟机 内核版本 3.19.0-15-generic 漏洞来源 /fs/overlayfs/inode.c Before 201 ...

  5. CentOS7--64安装python的psutil模块

    1.以root身份登陆CentOS依次 执行以下命令: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.g ...

  6. Tree( 树) 组件[4]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一.方法列表 //部分方法onClick : funct ...

  7. MVC,jquery异步

    创建一个Ajax控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...

  8. 一小时搞定DIV+CSS布局-固定页面开度布局

    本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...

  9. java对象Integer不能引用传递

    java对象Integer不能引用传递 /** * The value of the <code>Integer</code>. * * @serial */ private ...

  10. HTML5 Canvas前台压缩图片并上传到服务器

    1.前台代码: <input id="fileOne" type="file" /> <input id="btnOne" ...