leetcode -- Merge k Sorted Lists add code

时间:2021-11-30 09:02:38

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

[解题思路]

以前的解法的时间复杂度过高,通过在网上搜索,得到优化的时间复杂度:O(n*lgk)

维护一个大小为k的最小堆,每次得到一个最小值,重复n次

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode head = null;
int len = lists.size();
if(len <= 1)
return null;
head = merge2List(lists.get(0), lists.get(1));
for(int i = 2; i < len; i++){
head = merge2List(lists.get(i), head);
}
return head; } public ListNode merge2List(ListNode node1, ListNode node2){
ListNode head = null;
ListNode tmp = head;
while(node1 != null && node2 != null){
if(node1.val <= node2.val){
ListNode node = new ListNode(node1.val);
tmp = node;
tmp = tmp.next;
} else {
ListNode node = new ListNode(node2.val);
tmp = node;
tmp = tmp.next;
}
node1 = node1.next;
node2 = node2.next;
} while(node1 != null){
ListNode node = new ListNode(node1.val);
tmp = node;
tmp = tmp.next;
node1 = node1.next;
} while(node2 != null){
ListNode node = new ListNode(node2.val);
tmp = node;
tmp = tmp.next;
node2 = node2.next;
}
return head; }
}

上一版本有bug,修复如下:

 public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode head = null;
int len = lists.size();
if(len == 0)
return null;
else if(len == 1){
return lists.get(0);
}
head = merge2List(lists.get(0), lists.get(1));
for(int i = 2; i < len; i++){
head = merge2List(lists.get(i), head);
}
return head; } public ListNode merge2List(ListNode node1, ListNode node2){
ListNode head = new ListNode(Integer.MIN_VALUE);
ListNode tmp = head;
while(node1 != null && node2 != null){
if(node1.val <= node2.val){
ListNode node = new ListNode(node1.val);
tmp.next = node;
tmp = tmp.next;
node1 = node1.next;
} else {
ListNode node = new ListNode(node2.val);
tmp.next = node;
tmp = tmp.next;
node2 = node2.next;
}
} if(node1 != null){
tmp.next = node1;
} if(node2 != null){
tmp.next = node2;
}
head = head.next;
return head; }
}

http://blog.csdn.net/zyfo2/article/details/8682727

http://tech-wonderland.net/blog/leetcode-merge-k-sorted-lists.html

leetcode -- Merge k Sorted Lists add code的更多相关文章

  1. LeetCode&colon; Merge k Sorted Lists 解题报告

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

  2. &lbrack;LeetCode&rsqb; Merge k Sorted Lists 合并k个有序链表

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

  3. LeetCode&colon;Merge k Sorted Lists

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

  4. LeetCode——Merge k Sorted Lists

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

  5. LeetCode Merge k Sorted Lists &lpar;链表&rpar;

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

  6. &lbrack;Leetcode&rsqb; Merge k sorted lists 合并k个已排序的链表

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

  7. Leetcode&colon;Merge k Sorted Lists分析和实现

    题目大意是传入一个链表数组lists,每个链表都由若干个链接的链表结点组成,并且每个链表结点记录一个整数.题目保证传入的链表中的整数按从小到大进行排序. 题目要求我们输出一个新的链表,这个链表中应该包 ...

  8. LeetCode Merge k Sorted Lists 解决报告

    https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...

  9. LeetCode —— Merge k Sorted Lists

    /* ** 算法的思路: ** 1.将k个链表的首元素进行建堆 ** 2.从堆中取出最小的元素,放到链表中 ** 3.如果取出元素的有后续的元素,则放入堆中,若没有则转步骤2,直到堆为空 */ #in ...

随机推荐

  1. sum&lpar;&rpar; 函数

    sum()的参数是一个list 例如: sum([1,2,3])

  2. asp&period;net&lpar;C&num;&rpar;清除全部Session与单个Session

    Session.Abandon();//清除全部SessionSession["UserName"] = null;Session.Remove("UserName&qu ...

  3. DedeCMS批量替换栏目文件保存目录的方法

    学点sql还是很有必要的.   有时候由于栏目太多,但是要修改一下栏目的保存目录.一个一个修改真的有点费事和慢.所以想了一个方法来批量修改栏目的保存目录.就是批量替换: update dede_arc ...

  4. laravel项目thinksns-plus安装出现RuntimeException Symlink from &ast; to &ast; failed错误

    今天xshell安装thinksns-plus的laravel项目时出现了一个错误, [RuntimeException] Symlink from "/root/www.z5w.net/t ...

  5. Html中video的属性和方法大全

    <video>标签的属性 src :视频的属性 poster:视频封面,没有播放时显示的图片 preload:预加载 autoplay:自动播放 loop:循环播放 controls:浏览 ...

  6. CRC、MD5和SHA1的区别?

    什么是CRC校验?CRC即循环冗余校验码:是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错功能,对数据进行多项式计算,并将 ...

  7. 看了一下unity5&period;6的新功能 以及Timeline

    3月31日unity5.6发布,然而timeline(前sequence模块)被delay到unity 2017.上个星期官方又发布了unity 2017的beta版本 抽空看了下 (unity5.6 ...

  8. Python3基础 os&period;path&period;splitext 处理文件名,得到文件名&plus;扩展名

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. hdu 3045 斜率优化DP

    思路:dp[i]=dp[j]+sum[i]-sum[j]-(i-j)*num[j+1]; 然后就是比较斜率. 注意的时这里j+t<=i: #include<iostream> #in ...

  10. Knockout v3&period;4&period;0 中文版教程-3-监控-通过监控创建视图模型(下)

    6. 显式订阅监控 你通常不需要手动设置订阅,所以初学者应该跳过这一节. 对于高级用户,如果你想注册自己的订阅来监控通知变化,你可以使用 subscribe函数,比如: myViewModel.per ...