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

使用分治法,时间复杂度是nlogk, n是所有元素个数的总和,k是k个lists,

这种方法和依次merge每一个list的方法的时间复杂度不同。

如果第一个list有n-k个元素,其余每个list是1个元素,

两两分治合并,每个元素参与了logk次合并,

如果是依次合并(第一个和第二个合并,之后的结果和第三个合并,以此类推),n-k个元素需要参与k次合作,

这样复杂度就会增加。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists.length == 0){
return null;
}
return mergeHelper(lists, 0, lists.length - 1);
} public ListNode mergeHelper(ListNode[] list, int start, int end){
if (start == end){
return list[start];
} int mid = start + (end - start) / 2;
ListNode left = mergeHelper(list, start, mid);
ListNode right = mergeHelper(list, mid + 1, end);
return mergeTwoSortedList(left, right);
} public ListNode mergeTwoSortedList(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while(list1 != null && list2 != null){
if(list1.val < list2.val) {
tail.next = list1;
tail = list1;
list1 = list1.next;
}else {
tail.next = list2;
tail = list2;
list2 = list2.next;
}
} if(list1 != null) {
tail.next = list1;
} if(list2 != null) {
tail.next = list2;
}
return dummy.next;
}
}

画了一张图

Merge k Sorted Lists Leetcode Java的更多相关文章

  1. 23. Merge k Sorted Lists - LeetCode

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

  2. Merge k Sorted Lists [LeetCode]

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

  3. Merge Two Sorted Lists leetcode java

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

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

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

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

  6. LeetCode: Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  7. LeetCode 023 Merge k Sorted Lists

    题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and ...

  8. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  9. 【LeetCode练习题】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

随机推荐

  1. docker mysql

    创建mysql docker容器 github上有一个专门的docker-libary项目,里面有各种各样常用的docker镜像,可以做为学习的示例,今天研究下其中mysql镜像的用法,国内镜像dao ...

  2. aircrack-ng test

    Aircrack-ng工具包有很多工具,我用到的工具主要有以下几个: airmon-ng 处理网卡工作模式 airodump-ng 抓包 aircrack-ng 破解 aireplay-ng 发包,干 ...

  3. Java数据库——使用元数据分析数据库

    在JDBC中提供了DatabaseMetaData和ResultSetMetaData接口来分析数据库的元数据. DatabaseMetaData 使用DatabaseMetaData取得数据库的元信 ...

  4. Java链表

    链表的每一个节点中除了要保存本身的内容之后,还要保存下一个节点的引用 简单的建立一个链表,表头是root //============================================= ...

  5. (转)HBase 的原理和设计

    转自:HBase的原理和设计 HBase架构:

  6. js随机从数组中取出几个元素

    JS如何从一个数组中随机取出一个元素或者几个元素. 假如数组为 var items = ['1','2','4','5','6','7','8','9','10']; 1.从数组items中随机取出一 ...

  7. Mac技巧

    ////////////////////////////////////////////////////////////////////////////////PC键盘在Mac上的映射Ctrl = C ...

  8. 取出return array() 数组内容

    d.php文件 return array( "0" => 内容一, "1" => 内容二, "2" => 内容三, &qu ...

  9. 学海无涯的整理Ing..........

    1.文章:Understanding JavaScript Function Invocation and “this” http://yehudakatz.com/2011/08/11/unders ...

  10. centos 7.0 安装vim

    用的最小化 安装 看看跟VI命令有何区别 后面会记录使用经验 [root@localhost conf]# yum -y install vim 已加载插件:fastestmirror base | ...