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. 精心挑选的HTML5/CSS3应用及源码

    这段时间我已经为大家分享了不少关于HTML5应用和jQuery插件了,先来回顾一下: 炫酷霸气的HTML5/jQuery应用及源码 干货分享 超炫丽的HTML5/jQuery应用及代码 绚丽而实用的j ...

  2. PHP替换回车换行的三种方法

    一个小小的换行,其实在不同的平台有着不同的实现,为什么要这样,世界是多样的! 本来在Unix世界换行用/n来代替换行, Windows为了体现不同,就用/r/n, 更有意思的是,Mac中又用了/r. ...

  3. dropwizard metrics - 基本使用介绍

    之前在healthcheck中介绍了怎样通过metrics lib往系统中增加一些简单的健康侦測.如今讲讲dropwizard metrics更重要的部分.记录系统的度量信息. dropwizard提 ...

  4. ZooKeeper学习之-Zookeeper简单介绍(一)

    一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术主要用来解决分布式环境当中多个进程之间的同 ...

  5. 从debian9、ubuntu18.04的deb包依赖来看,似乎不是那么好!!

    i386体系的依赖.典型的libuuid1依赖问题,需要downgrade保持版本一致! amd64的rpm依赖低版本的perl-base,需要downgrade保持版本一致!

  6. git 强制刷新,放弃更改

    git fetch --all  git reset --hard origin/master

  7. 如果BarTender出现卸载不干净的问题如何处理

    自从BarTender 2016出了之后,好多小伙伴都想试试新功能咋样,这就意味着首先要卸载电脑上旧版BarTender.然而就是这个操作,难倒了好一批人,他们表示BarTender卸载卸不干净,不仅 ...

  8. 元素随屏幕滚动到顶部固定js效果

    网站中常见这种效果,某个广告或详情页切换tab,当屏幕向下移动时,该元素会停留在浏览器最顶部,下面ecshop模板中心教您实现js代码: 案例图: 1.首先在页面上找到该元素  加上 id =&quo ...

  9. A股最新的自由现金流和折现估值查询

    A股最新的自由现金流折现估值,利用自由现金流折现的经典公式,采用 8%.9%.10%.11%.12%.15% 等贴现率来进行估值. SH600000:浦发银行的最新自由现金流和折现估值模型: 浦发银行 ...

  10. 8 -- 深入使用Spring -- 4...3 AOP的基本概念

    8.4.3 AOP的基本概念 AOP从程序运行角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中各个步骤,希望以更更好的方式来组合业务处理的各个步骤. AOP框架并不与特定的代码耦合 ...