[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 ...
随机推荐
- PythonStudy——比较运算符 Comparison operator
1.运算结果为bool类型 print(3 > 5) Output: False 2.可以连比 num = 10 print(1 < num < 20)# 与之上的等价写法是: pr ...
- centos7生产环境下openssh升级
由于生产环境ssh版本太低,导致使用安全软件扫描时提示系统处于异常不安全的状态,主要原因是ssh漏洞.推荐通过升级ssh版本修复漏洞 因为是生产环境,所以有很多问题需要注意.为了保险起见,在生产环境下 ...
- 01python语言程序设计基础——初识python
1.python的字符串中format函数用法 format 函数可以接受不限个参数,位置可以不按顺序. In [2]: "{} {}".format("hello& ...
- ipc基础
ipc对象持久性 进程持久性:具有这种持久性的对象在持有它的最后一个进程关闭了该对象为止 内核持久性:这种IPC对象一直存在直到内核重新自举或显示删除该对象为止 文件系统持久性:具有这种持久性的对象只 ...
- mongodb集群配置分片集群
测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 mongo01 从服务器IP地址:192.168.197.22 mongo02 从服务器IP地址: ...
- 【python】self & cls
转自 python中self,cls 普通的方法,第一个参数需要是self,它表示一个具体的实例本身. 如果用了staticmethod,那么就可以无视这个self,而将这个方法当成一个普通的函数使用 ...
- Tomcat性能调优后, 启动出现警告问题 [did not find a matching property.]
http://blog.csdn.net/dracotianlong/article/details/8963594 Tomcat性能调优后, 启动出现警告问题 [did not find a mat ...
- 黄聪:windows下使用xampp3.2.2配置多个监听端口和不同的网站目录
windows下使用xampp3.2.2配置多个监听端口和不同的网站目录 一:配置Apache文件httpd.conf 打开Apache的配置文件httpd.conf,可以通过点击xampp的Apac ...
- 为嵌入式全志V3s荔枝派板卡添加USB MT7601U(小米随身WIFI)驱动
折腾了了一天终于scan出环境热点了,感觉本来挺简单的事情,网上教程一大把还费了一天的劲,很丧.不过网上教程虽多,但是还还是有些不同之处的,现在特意总结一下 全志V3s荔枝派板卡 添加该驱动的过程. ...
- 8、sort排序中比较函数的几种应用方式
1.待排序中的元素作数组的下标或map的键值 例题:PAT甲级_1141 PAT Ranking of Institutions #include<bits/stdc++.h> using ...