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个有序链表的更多相关文章

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

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

  3. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  4. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

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

  6. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

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

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

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

随机推荐

  1. pandas Dataframe 取某行

    In [1]: df = DataFrame(randn(5,2),index=range(0,10,2),columns=list('AB')) In [2]: df Out[2]: A B 0 1 ...

  2. 网络之 Iptables总结

    查询iptables -L 默认 filter表iptables -L -t filteriptables -L -t natiptables -L -t mangle Filter表service ...

  3. 【转】C++中嵌入python程序——参数传递

    C++中嵌入python程序——参数传递 前面两篇博客已经介绍如何在C++中嵌套使用 python,但是在实际使用中,我们需要向python传递各种各样的参数,这样的程序才具有更高的灵活性.下面简单介 ...

  4. centos7-软件安装-jdk1.8

    JDK1.8下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 安装目录 ...

  5. 分布式CAP定理,为什么不能同时满足三个特性?

    在弄清楚这个问题之前,我们先了解一下什么是分布式的CAP定理. 根据百度百科的定义,CAP定理又称CAP原则,指的是在一个分布式系统中,Consistency(一致性). Availability(可 ...

  6. sql使用实例

    将另一表中的合计值保存到指定字段,并将空值赋0 update ShopInfo set JLRunningWater =(select COALESCE(sum(v.TotalMoney),0) as ...

  7. Java笔记Spring(二)

    spring-core 通过Gradle构建工具,转换包的命名空间为org.springframework下 cglib包,net.sf.cglib -> org.springframework ...

  8. python3笔记<一>基础语法

    随着AI人工智能的兴起,网络安全的普及,不论是网络安全工程师还是AI人工智能工程师,都选择了Python.(所以本菜也来开始上手Python) Python作为当下流行的脚本语言,其能力不言而喻,跨平 ...

  9. Requests对HTTPS请求验证SSL证书

    SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secure socket layer(SSL)安全协议是由Netscape Communication公司设计开发.该安全协议主 ...

  10. jQuery.ajax()调用asp.net后台方法(非常重要)

    http://www.cnblogs.com/zxhoo/archive/2011/01/30/1947752.html 用JQuery的$.ajax()可以很方便的调用asp.net的后台方法. 先 ...