Problem: 已知两个有序链表(链表中的数值递增排列)。将这两个链表进行合并操作,并且满足递增排列 即合并后仍为有序链表。
 
 
递归进行合并操作。mergeTwoList(ListNode list1, ListNode list2)
     当list1==null时 return list2;
     当list2==null时 return list1;
     当list1.val <=list2.val时 ,mergeHead等于list1;mergeHead.next = mergeTwoList(list1.next,list2);
     当list1.val > list2.val 时, mergeHead等于list2;  mergeHead.next = mergeTwoList(list1,list2.next);
 
 
参考代码:
 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 两个有序链表整合
*/
public class Solution21 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null){
return l2;
}
if(l2 == null){
return l1;
} ListNode mergeHead;
if(l1.val <= l2.val){
mergeHead = l1;
mergeHead.next = mergeTwoLists(l1.next, l2);
}
else{
mergeHead = l2;
mergeHead.next = mergeTwoLists(l1, l2.next);
}
return mergeHead;
} }
 

LeetCode 21 Merge Two Sorted Lists (有序两个链表整合)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

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

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

  5. [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 ...

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

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

  7. [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 ...

  8. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

  9. 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 ...

随机推荐

  1. jquery 实现 Json 的一些转换方法

    有一个json 字符串 1)要判断该字符串是否是 json 格式 方法:将其转换成json对象,如果报异常,则不是,否则就是json格式 function isJsonFormat(str) { tr ...

  2. memcached系列之二

    存储命令 1.set命令 语法: set key flags exptime bytes [noreply] value 参数说明: key: flags: exptime: bytes: norep ...

  3. asp.net网页中添加年月日时分秒星期。

    html代码如下: 现在是<span id="TimeSpan"></span> <script type="text/javascript ...

  4. CentOS6.5下安装iRedMail中需要解决的问题

    iRedMail是个专门用于Redhat/CentOS下的企业Mail服务集成安装软件包,本来只要有干净的系统就可以轻松安装,无奈国内网络状况和墙头众多,安装中也有很多问题需要解决,下面记录的都是我安 ...

  5. Java编程思想学习笔记——接口

    1.抽象类和抽象方法 抽象方法:不完整的,仅有声明而没有方法体. abstract void f(); 抽象类:包含抽象方法的类.(若一个类包含一个或多个抽象方法,则该类必须限定为抽象的.) 1.用抽 ...

  6. MathType输入框怎么调整

    在用MathType编辑公式编辑器时,除了可以对MathType工具栏的显示比例进行调整以外,还可以对编辑时的输入框进行调整.这样在编辑的过程中,工具栏可以看得很清楚,同时框输入框也可以看得很清楚,这 ...

  7. NPOI把Excel导入到数据库

    二,把Excel中的数据导入到数据库的具体步骤: protected void Button1_Click(object sender, EventArgs e)        {           ...

  8. js中的string.format

    String.prototype.format = function(args) { var result = this; if (arguments.length > 0) { if (arg ...

  9. Nginx配置中文域名

    今天碰到一个好玩的问题,还以为是nginx的缓存,各种清理就差把nginx卸载了,后来想想不对应该是中文域名的问题,对中文进行编码,搞定,如下: ... server { listen 80; ser ...

  10. GoF--装饰者设计模式

    顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例. 装饰器模式的应用场景: 1.需要扩展一个类的功能. 2.动态的为 ...