leetcode -- Merge k Sorted Lists add code
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的更多相关文章
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- LeetCode:Merge k Sorted Lists
题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
- LeetCode Merge k Sorted Lists (链表)
题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- Leetcode:Merge k Sorted Lists分析和实现
题目大意是传入一个链表数组lists,每个链表都由若干个链接的链表结点组成,并且每个链表结点记录一个整数.题目保证传入的链表中的整数按从小到大进行排序. 题目要求我们输出一个新的链表,这个链表中应该包 ...
- LeetCode Merge k Sorted Lists 解决报告
https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...
- LeetCode —— Merge k Sorted Lists
/* ** 算法的思路: ** 1.将k个链表的首元素进行建堆 ** 2.从堆中取出最小的元素,放到链表中 ** 3.如果取出元素的有后续的元素,则放入堆中,若没有则转步骤2,直到堆为空 */ #in ...
随机推荐
- ubuntu 下 数学库编译链接时找不到各种数学问题解决方法 can not fon atan 等等
解决参考 http://askubuntu.com/questions/190246/ld-cannot-find-math-library you should use -lm at the end ...
- JQuery:各种操作表单元素方法小结
来源:http://www.ido321.com/1220.html 表单元素无处不在,已然成了Web应用不可或缺的一个部分.对表单最最最常见的操作就是获取表单元素的值或者更改表单元素的值.那在JQu ...
- JQuery笔记:JQuery和JavaScript的联系与区别
来源:http://www.ido321.com/1019.html ps:LZ觉得这个标题有点大了,超出了能力范围,不喜勿碰.目前只记录LZ能力范围内的,日后持续补充. 一.JQuery对象和DOM ...
- [Java Code] 时间维度循环生成代码片段
public static void main(String[] args) throws ParseException { String str = "20140301"; St ...
- [Hive - LanguageManual] Hive Concurrency Model (待)
Hive Concurrency Model Hive Concurrency Model Use Cases Turn Off Concurrency Debugging Configuration ...
- ubuntu下通过pip安装pyside
首先安装相关库 sudo apt-get install build-essential git cmake libqt4-dev libphonon-dev python2.7-dev libxml ...
- javaScript 类型判断
直接上例子: 1 判断是否为数组类型 2 判断是否为字符串类型 3 判断是否为数值类型 4 判断是否为日期类型 5 判断是否为函数 6 判断是否为对象 1 判断是否为数组类型 linenum < ...
- jquery easyui的异步tree
1.创建一个简单的tree 结果如图: <script> $(function(){ $('#tt').tree(){ url:'要提交的url地址', checkbox:true, li ...
- HDU 2063 过山车(二分匹配入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.ne ...
- AppServ 配置还是成功了
安装就不说了,一键式安装,还不错. 首先,登陆密码吧,用户是root ,密码是空: 之前创建的databases都还在, 可以用phpmyadmin管理,不在输入复杂的命令了, 方便,继续鼓捣php.