`
huntfor
  • 浏览: 194914 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode]Merge k Sorted Lists

 
阅读更多

新博文总结了三种算法,新博文地址:[leetcode]Merge k Sorted Lists

Merge k Sorted Lists

 

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

 K路归并,不知道大家对2路归并还有木有印象,如果木有的话,请戳这里

其实K路归并,就是进行k - 1 次二路归并,完全一样。

代码如下:

public ListNode mergeKLists(ArrayList<ListNode> lists){
		ListNode result = new ListNode(0);
		if(lists == null || lists.size() == 0 ){
			return null;
		}
		if(lists.size() == 1){
			return lists.get(0);
		}
		result = merge2List(lists.get(0),lists.get(1));
		for(int i = 2; i < lists.size();i++){
			result = merge2List(result,lists.get(i));
		}
		return result;
	}

	private ListNode merge2List(ListNode l1,ListNode l2){
		ListNode l1Tail = l1;
		ListNode l2Tail = l2;
		ListNode list = new ListNode(0);
		ListNode tail = list;
		while(l1Tail != null || l2Tail != null){
			if(l1Tail == null){
				tail.next = l2Tail;
				break;
			}
			if(l2Tail == null){
				tail.next = l1Tail;
				break;
			}
			if(l1Tail.val < l2Tail.val){
				tail.next = l1Tail;
				l1Tail = l1Tail.next != null ? l1Tail.next : null;;
			}else{
				tail.next = l2Tail;
				l2Tail = l2Tail.next != null ? l2Tail.next : null;
			}
			tail = tail.next;
					
		}
		return list.next;
	}

 

分享到:
评论
4 楼 huntfor 2014-06-28  
249326109 写道
huntfor 写道
249326109 写道
这种方法的复杂度有没有算过,想想更优的的解法。

为毛跑到开源中国开博客啊,Iteye干嘛不用?

一天只能发5篇博客怎么破。。

写草稿,第二天发,这个没办法
3 楼 249326109 2014-06-27  
huntfor 写道
249326109 写道
这种方法的复杂度有没有算过,想想更优的的解法。

为毛跑到开源中国开博客啊,Iteye干嘛不用?

一天只能发5篇博客怎么破。。
2 楼 huntfor 2014-06-25  
249326109 写道
这种方法的复杂度有没有算过,想想更优的的解法。

为毛跑到开源中国开博客啊,Iteye干嘛不用?
1 楼 249326109 2014-06-25  
这种方法的复杂度有没有算过,想想更优的的解法。

相关推荐

    LeetCode Merge 2 Sorted Lists解决方案

    LeetCode Merge 2 Sorted Lists解决方案

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters ...23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli

    leetcode中文版-LeetCode:力码

    leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...

    MergeTwoSortedLinkedList.java

    【Leetcode】Merge Two Sorted Lists

    leetcode分类-leetcode:leetcode问题的代码

    leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median ...Sorted ...#23:Merge k Sorted Lists

    lrucacheleetcode-leetcode-journal:记录所有LeetCode挑战的存储库

    lru缓存leetcode 力扣日记 欢迎来到我的 LeetCode 期刊库。 我的每个问题都将包括一个初始计划...k Sorted Lists (Hard) 31. Next Permutation (Medium) 34. Find First and Last Position of Element in Sorted Arr

    leetcode答案-LeetCode:Swift中的LeetCode

    Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 ...

    leetcode添加元素使和等于-leetcode:我的leetcode解决方案

    leetcode添加元素使和等于 经验教训 一定要吧功能尽量细化为函数,这样一者做题思路比较清晰,二者可以在某些情况下直接return值。 如果输入的形式是一个序列,则可以想想:分治、动规、贪婪,一般不建议搜索,因为...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode ...Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3

    leetcode2-Leetcode:Leetcode_answer

    leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。...Merge Two Sorted Lists JavaScript O(n)

    LeetCode最全代码

    26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...

    多线程leetcode-leetcode-java:leetcode上的题解,基于java语言

    多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium ...Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water

    leetcode中文版-LeetCode:LeetcodeC++/Java

    leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash 2 Add Two Numbers 两数相加 math 3 Longest Substring ...Sorted ...Merge Two Sorted Lists 合并两个有序链表 lin

    leetcode2sumc-LeetCode:LeetCode的一些题目

    Merge Two Sorted Lists 83 Easy Remove Duplicates from Sorted List 141 Easy Linked List Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked ...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode跳跃-leetcode:leetcode一天一次

    leetcode 跳跃 leetcode 动态规划,背包问题 01背包问题:416. Partition Equal Subset Sum 最长回文:5. Longest Palindromic ...Merge ...Sorted Lists 回文 双指针判断回文:680. 验证回文字符串 Ⅱ

    LeetCode 23. 合并K个排序链表

    链接:https://leetcode-cn.com/problems/merge-k-sorted-lists 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路 很简单,因为是小的先插入,使用优先队列实现。把k个链表当前第一个...

    java二叉树算法源码-algorithm-primer:algorithmprimer-算法基础、Leetcode编程和剑指offer,Ja

    java二叉树算法源码 algorithm-primer algorithm primer - 算法基础、Leetcode编程、剑指offer ...Merge Two Sorted Lists Easy 141 判断链表是是否存在环 Linked List Cycle Easy 142 环形链表II Linked List Cycle I

    :猴子:LeetCode,剑指提供刷题笔记(C / c++, Python3实现)

    LeetCode 原创文章每周最少两篇,后续最新文章会在首发,视频首发,大家可以加我进交流群,技术交流或提意见都可以,欢迎Star!...Merge Two Sorted Lists 83 * Remove Duplicates from Sorted Lis

    leetcode答案-LeetCode-Trip:LeetCode刷题代码,大佬勿入

    Merge Two Sorted Lists] [53. Maximum Subarray] [70. Climbing Stairs] [101. Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is ...

Global site tag (gtag.js) - Google Analytics