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的更多相关文章

  1. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  2. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  3. 刷题23. Merge k Sorted Lists

    一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...

  4. 23. Merge k Sorted Lists - LeetCode

    Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...

  5. 蜗牛慢慢爬 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 ...

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

  7. [leetcode 23]Merge k Sorted Lists

    1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  8. 23. Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. = ...

  9. Java [leetcode 23]Merge k Sorted Lists

    题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...

随机推荐

  1. 学生信息的添加 Java web简单项目初试(失败)

    题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...

  2. composer命令卡慢,使用国内源

    执行composer install.update 和require的时候,遇到卡住不动的情况,可以切换到国内阿里云的源 composer config -g repo.packagist compo ...

  3. MySQL优化查询相关

    [查询优化相关] 1.如何定位相关慢的查询: a.可以开启慢查询日志,也可以使用show profiles 去记录相关查询到一个临时表再分析. b.show processlist  看看有没有大量等 ...

  4. python学习笔记-模块和包

    模块导入方法 1.import 语句 import module1[,module2[,...moduleN]] 当我们使用import语句的时候,Python解释器是怎么找到对应对文件对呢?答案是解 ...

  5. shell脚本判断进程是否运行

    zzx@zzx120:~$ if ps aux | grep "python"|grep -v grep > /dev/null             #$?的值不同 th ...

  6. [C/C++]C/C++计算代码的运行时间

    有很多时候,实现一个功能后可能不仅仅要效果,还要效率,如果可以在极短的时间内完成一个功能那当然是最好不过的啦,但是可能经常会事与愿违. 这里就写一下,都可以怎样用C/C++或者Qt的方法来测试代码的运 ...

  7. symmetry methods for differential equations,exercise 1.4

    tex文档: \documentclass[a4paper, 12pt]{article} % Font size (can be 10pt, 11pt or 12pt) and paper size ...

  8. h5-过度

    1.过度的基本介绍及写法 .div{ width: 200px; height: 200px; background-color: red; position: absolute; left: 100 ...

  9. 编程作业1.1——sklearn机器学习算法系列之LinearRegression线性回归

    知识点 scikit-learn 对于线性回归提供了比较多的类库,这些类库都可以用来做线性回归分析. 我们也可以使用scikit-learn的线性回归函数,而不是从头开始实现这些算法. 我们将scik ...

  10. UI Automation编程辅助工具Inspect的下载和使用

    UIAutomation微软提供的UI自动化库,主要用AutomationElement类来表示UI 自动化目录树中的一个UI自动化元素,.NET Windows的窗体应用程序和WPF应用程序. In ...