[LC] 23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6 Solution 1:
Time: O(Nlgk)
Space: O(N)
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val);
// need to check list null as well
for(ListNode list: lists) {
if (list != null) {
pq.add(list);
}
}
while(!pq.isEmpty()) {
ListNode node = pq.poll();
cur.next = node;
cur = cur.next;
if (node.next != null) {
pq.add(node.next);
}
}
return dummy.next;
}
}
solution 2:
Time: O(Nlgk)
Space: O(N)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
ListNode res = sort(lists, 0, lists.length - 1);
return res;
} private ListNode sort(ListNode[] lists, int low, int high) {
if (low >= high) {
return lists[high];
}
int mid = low + (high - low) / 2;
ListNode left = sort(lists, low, mid);
ListNode right = sort(lists, mid + 1, high);
return merge(left, right);
} private ListNode merge(ListNode aNode, ListNode bNode) {
if (aNode == null) {
return bNode;
}
if (bNode == null) {
return aNode;
}
if (aNode.val <= bNode.val) {
aNode.next = merge(aNode.next, bNode);
return aNode;
} else {
bNode.next = merge(aNode, bNode.next);
return bNode;
}
}
}
[LC] 23. Merge k Sorted Lists的更多相关文章
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- 刷题23. Merge k Sorted Lists
一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...
- 23. Merge k Sorted Lists - LeetCode
Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- [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 ...
- [leetcode 23]Merge k Sorted Lists
1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- 23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. = ...
- Java [leetcode 23]Merge k Sorted Lists
题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...
随机推荐
- 查找ARP攻击源
问题: 内网有电脑中了ARP病毒,但是网络拓扑比较复杂.电脑数量较多,排查起来很困难.有什么方法可以找出ARP攻击源?[推荐3]排查方法: 1.使用Sniffer抓包.在网络内任意一台主机上运行抓包软 ...
- CAD快捷键大全
- 微信公众号关联小程序AppID是什么
微信公众平台appid在哪 1.appid和appsecret是微信公众平台服务号才有的,如果自己家的公众平台不是服务号,需要升级为服务号. 2.登录服务号,登录“服务”条目,“服务中心”如图. 3. ...
- idhttp文件的上传和下载
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- JavaScript 闭包究竟是什么JavaScript 闭包究竟是什么
用JavaScript一年多了,闭包总是让人二丈和尚摸不着头脑.陆陆续续接触了一些闭包的知识,也犯过几次因为不理解闭包导致的错误,一年多了资料也看了一些,但还是不是非常明白,最近偶然看了一下 jQue ...
- 显著水平|区间估计|假设检验|显著性|第一类错误|Ⅱ类错误|β错误|t检验|连续性矫正|二项分布的假设检验|样本百分率|
第三章 假设检验 区间估计与假设检验的基本区别? 上一章中讨论了置信区间的估计方法.它是利用样本数据,以抽样总体的分布为理论基础,用一定的概率保证来计算出原总体中未知参数的区间范围.特别值得注意的是: ...
- 测试Java程序执行耗费的时间
package test; public class Main { public static void main(String[] args) { long start = System.curre ...
- JavaScript详解(三)
JavaScript的数组 JavaScript中的数组具有相当的灵活性,除了能存储数据外,还提供了一系列的属性和方法.因为JavaScript本身是一个弱类型语言,故其数组不会限制存放数据的类型. ...
- SpringBoot 2.2.2集成ElasticSearch 7.5.1
前言:现在公司有一个项目要用到检索功能,检索上面现在最常用的是Solr/ES,最后经过对比选择了ElasticSearch开源组件包,因为这个是公司的一个产品项目,技术版本当然要用最新的啦,最后完全确 ...
- Graph & Trees3 - 二分图
\[二分图略解\] \[By\;TYQ\] 二分图定义: \(f(i,L) = [a \in L\;\text{&}\;\forall b \in a.to \;\text{,}\; b \n ...