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 ...
随机推荐
- 浅谈javascript的变量作用域
1.变量遵循先声明再使用. console.log(b); b=123; 代码运行结果: Uncaught ReferenceError: b is not defined 2.方法内定义的局部变量外 ...
- flash recovery area配置
检查数据库是否开启闪回: SQL> select flashback_on from v$database; FLASHBACK_ON ----------------------------- ...
- Tkinter教程之Event篇(2)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823548 '''Tkinter教程之Event篇(2)''''''5.测试离开(Leave) ...
- Tkinter教程之Text(2)篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811347 '''Tkinter教程之Text(2)篇''''''6.使用tag来指定文本的属 ...
- HDU 5531 Rebuild (2015长春现场赛,计算几何+三分法)
Rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- JSF 2 textarea example
In JSF, you can use the <h:inputTextarea /> tag to render a HTML textarea field. For example, ...
- Spring Object/XML mapping example
In this tutorial, we will extend last Maven + Spring hello world example by adding JDBC support, to ...
- #elif
http://baike.sogou.com/v72031124.htm?fromTitle=%23elif #else指令用于某个#if指令之后,当前面的#if指令的条件不为真时,就编译#else后 ...
- linux sar 命令详解
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...
- Linux下的进程管理
在操作系统系统中,进程是一个非常重要的概念. 一.Linux中进程的相关知识 1.什么是进程呢? 通俗的来说进程是运行起来的程序.唯一标示进程的是进程描述符(PID),在linux内核中是通过task ...