[leetcode]23. Merge k Sorted Lists归并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6
题意:
归并k个有序链表。
思路:
用一个最小堆minHeap,将所有链表的头结点放入
新建一个linkedlist存储结果
minHeap里面每remove一个node(最小堆保证了该元素为当前堆中最小), 就加到新建linkedlist中,并将该node.next加入到堆里
代码:
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
// special case
if(lists.length==0) return null;
//1. priorityqueue ((o1,o2)-> o1.val, o2.val) acsending order
PriorityQueue<ListNode> heap = new PriorityQueue<>((o1,o2)-> o1.val-o2.val);
//add each list's each node into the heap, in acsending order
int size = lists.length;
for(int i = 0; i<size; i++){
if(lists[i]!=null){
heap.add(lists[i]); //注意这步操作直接把list的每个节点都扔进了heap
}
}
//2. create a new linkedlist
ListNode fakeHead = new ListNode(-1);
ListNode current = fakeHead;
//3. add every node from priorityqueue's removing
while(heap.size()!=0){
ListNode node = heap.remove();
current.next = node;
current=current.next;
if(node.next!=null) heap.add(node.next);
}
return fakeHead.next;
}
}
[leetcode]23. 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 21. Merge Two Sorted Lists合并两个有序链表 (C++)
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- 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】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- 021 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 ...
随机推荐
- Xamarin Android 下拉列表
<Spinner android:id="@+id/spinner" android:layout_width="wrap_content" androi ...
- 使用maven-tomcat7-plugins时调试出现source not found解决
直接看下面的步骤: 步骤1: 步骤2: 步骤3: 步骤4:
- autotools
文章目录 原文地址 Autotools上手指南1--autoconf基本思想 Autotools上手指南2--autoscan生成configure.ac Autotools上手指南3--autohe ...
- glog学习(一):glog的编译及demo
windows平台: 1.下载glog代码.下载地址:https://github.com/google/glog 2.使用cmake工具,获得对应的工程文件sln. 3.打开sln文件,生成对应的l ...
- Spring Web常见面试问题
一.Web容器初始化过程 先初始化listener,然后是filter,然后是servlet. 二.Spring MVC项目中IOC容器关系 Web容器启动时通知ContextLoaderListen ...
- zookeeper启动时报Cannot open channel to X at election address Error contacting service. It is probably not running.
配置storm集群的时候出现如下异常: 2016-06-26 14:10:17,484 [myid:1] - WARN [SyncThread:1:FileTxnLog@334] - fsync-in ...
- koa2+log4js+sequelize搭建的nodejs服务
主要参考http://www.jianshu.com/p/6b816c609669这篇文章 npm安装使用国内taobao镜像,速度更快些 npm --registry https://registr ...
- centos7如何使用yum命令
参照https://www.cnblogs.com/zhongguiyao/p/9029922.html 参照https://blog.csdn.net/shuaigexiaobo/article/d ...
- centos7.6+samba+设置可读可写不可删权限
samba原文 https://www.cnblogs.com/muscleape/p/6385583.html 设置可读可写不可删权限原文: https://blog.51cto.com/guanh ...
- 上线啦,PP.io!
经过我们PPIO团队成员们在20天零13小时零14分的辛勤努力下,我们的官网终于上线了! 是的,14年前我们就是东半球第一个P2P技术团队.我们的征程始于2004年春天一个温暖的午后.寝室里笨拙的 ...