题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description

Problem: 给出k个有序的list, 将其进行合并得到一个有序的list
 
  对于给出的ListNode[] lists ,可以进行两两合并。divide and conquer 
  将list分为前后两部分,对前半部分再次进行分半操作,对后半部分进行分半操作,然后将其进行合并操作。
  合并操作也就是对两个list进行合并
  合并操作可以采用递归算法:当l1的值小于l2时,合并l1.next 和l2 返回 l1 ,对于l2同样如此
 
  注意两条判断
    if(l1==null) return l2;
    if(l2==null) return l1;
  这两条语句十分重要,因为当一个list为空,另一个不为空时,需要返回不为空的一个
 
  在这里的merger函数 返回的究竟是l1呢还是l2呢,取决于l1的第一个元素和l2的第一个元素哪一个小。 哪一个小返回哪一个。
    并且注意 l1或者在merger过程中一直指向list的头(改变的只是中间的连接)
 
  参考代码: 
 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 合并k个list
*/
public class Solution23 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public static ListNode mergeKLists(ListNode[] lists){
return partion(lists,0,lists.length-1);
} public static ListNode partion(ListNode[] lists,int s,int e){
if(s==e) return lists[s];
if(s<e){
int q=(s+e)/2;
ListNode l1=partion(lists,s,q);
ListNode l2=partion(lists,q+1,e);
return merge(l1,l2);
}else
return null;
} //This function is from Merge Two Sorted Lists.
public static ListNode merge(ListNode l1,ListNode l2){
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next=merge(l1.next,l2);
return l1;
}else{
l2.next=merge(l1,l2.next);
return l2;
}
}
}

LeetCode 23 Merge k Sorted Lists(合并k个有序链表)的更多相关文章

  1. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  2. [LeetCode]21. Merge Two Sorted Lists合并两个有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

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

  4. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  5. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  6. 21. Merge Two Sorted Lists(合并2个有序链表)

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  7. 021 Merge Two Sorted Lists 合并两个有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  8. [LeetCode] 21. Merge Two Sorted Lists 合并有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

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

  10. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

随机推荐

  1. C# 随机获取国内IP

    调用getRandomIp()方法即可Framework3.5 +使用LINQ public string getRandomIp() { /* int[][] 这个叫交错数组,白话文就是数组的数组. ...

  2. 查看Ubuntu的版本和系统版本

    命令行语句:lsb_release -a 命令行语句:uname -a

  3. 怎样解决Java Web项目更改项目名后报错

    作为企业级开发最流行的工具,用Myeclipse开发java web程序无疑是最合适的,有时候,我们需要web工程的项目名,单方面的改动工程的项目名是会报错的,那么该如何改web工程项目名呢? 简 单 ...

  4. hive 1.2 配置

    参考链接 http://www.cnblogs.com/yjmyzz/p/how-to-install-hive-1-2-0-on-mac.html

  5. 命令行编译C++程序

        使用命令行来编译C++程序,我们可以有两种方法:     方法一:     1. 依次打开开始程序->Visual Studio 2010 –>Visual Studio tool ...

  6. 安卓开发笔记——Broadcast广播机制(实现自定义小闹钟)

    什么是广播机制? 简单点来说,是一种广泛运用在程序之间的传输信息的一种方式.比如,手机电量不足10%,此时系统会发出一个通知,这就是运用到了广播机制. 广播机制的三要素: Android广播机制包含三 ...

  7. python 数据类型-字符串-对象和方法

    python的字符串有众多方法,可以在doc文档中查看 示例 转换开头字母为大写 c1="welcome to my python" >>> c1.capital ...

  8. Tensorflow物体检测(Object Detection)API的使用

    Tensorflow在更新1.2版本之后多了很多新功能,其中放出了很多用tf框架写的深度网络结构(看这里),大大降低了吾等调包侠的开发难度,无论是fine-tuning还是该网络结构都方便了不少.这里 ...

  9. LeetCode[Array]----3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  10. 6.824 Lab 5: Caching Extents

    Introduction In this lab you will modify YFS to cache extents, reducing the load on the extent serve ...