题目内容:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

题目分析:本题是要合并两个已经有序的单链表,思路很简单,有两种方法:非递归和递归。

题目代码:

(1)非递归:

  为方便操作,定义一个辅助的头节点,然后比较原来两个链表的头节点,将小的那一个加入到合并链表,最后,当其中一个链表为空时,直接将另一个链表接入到合并链表即可。

//public class LeetCode21 为测试
public class LeetCode21 {
    public static void main(String[] args) {
        ListNode m1=new ListNode(1),m2=new ListNode(3),m3=new ListNode(5);
        m1.next=m2;
        m2.next=m3;
        System.out.println("链表1:"+m1.val+"->"+m2.val+"->"+m3.val);
        ListNode n1=new ListNode(2),n2=new ListNode(4),n3=new ListNode(6);
        n1.next=n2;
        n2.next=n3;
        System.out.println("链表2:"+n1.val+"->"+n2.val+"->"+n3.val);
        ListNode result=new Solution().mergeTwoLists(m1, n1);
        if(result!=null){
            System.out.print("合并链表:"+result.val);
            ListNode resultNext=result.next;
            while(resultNext!=null){
                 System.out.print("->"+resultNext.val);
                 resultNext=resultNext.next;
             }
        }
    }
}
class Solution {
     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
         ListNode fakeHead=new ListNode(0);
         ListNode p=fakeHead;
         while(l1!=null&&l2!=null){
             if(l1.val<l2.val){
                 p.next=l1;
                 l1=l1.next;
             }else{
                 p.next=l2;
                 l2=l2.next;
             }
             p=p.next;
         }
         if(l1!=null) p.next=l1;
         if(l2!=null) p.next=l2;
         return fakeHead.next;
        }
}
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
    }

(2)递归:

//public class LeetCode21 为测试
public class LeetCode21 {
    public static void main(String[] args) {
        ListNode m1=new ListNode(1),m2=new ListNode(3),m3=new ListNode(5);
        m1.next=m2;
        m2.next=m3;
        System.out.println("链表1:"+m1.val+"->"+m2.val+"->"+m3.val);
        ListNode n1=new ListNode(2),n2=new ListNode(4),n3=new ListNode(6);
        n1.next=n2;
        n2.next=n3;
        System.out.println("链表2:"+n1.val+"->"+n2.val+"->"+n3.val);
        ListNode result=new Solution().mergeTwoLists(m1, n1);
        if(result!=null){
            System.out.print("合并链表:"+result.val);
            ListNode resultNext=result.next;
            while(resultNext!=null){
                 System.out.print("->"+resultNext.val);
                 resultNext=resultNext.next;
             }
        }
    }
}
class Solution {
     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
         if(l1==null) return l2;
         if(l2==null) return l1;
         if(l1.val<l2.val){
             l1.next=mergeTwoLists(l1.next, l2);
             return l1;
         }else{
             l2.next=mergeTwoLists(l1, l2.next);
             return l2;
         }
        }
}
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
    }

21. Merge Two Sorted Lists★的更多相关文章

  1. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

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

  3. 21. Merge Two Sorted Lists【easy】

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

  4. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  5. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  6. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  7. C# 写 LeetCode easy #21 Merge Two Sorted Lists

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

  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] 21. Merge Two Sorted Lists (Easy)

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

  10. [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. SonarQube安装文档

    1.SonarQube 1.1 SonarQube介绍 SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误. SonarQube是否可以使用自定义规则由开发人员的开 ...

  2. 【做题】ECFinal2018 J - Philosophical … Balance——dp

    原文链接 https://www.cnblogs.com/cly-none/p/ECFINAL2018J.html 题意:给出一个长度为\(n\)的字符串\(s\),要求给\(s\)的每个后缀\(s[ ...

  3. UVA11922 Permutation Transformer

    思路 直接使用FHQ Treap维护即可 代码 #include <cstdio> #include <cstring> #include <algorithm> ...

  4. springmvc学习之jdk版本,tomcat版本,spring版本

    使用的软件是myeclipse2018,jdk8,tomcat9.0,spring3.2.0 以上为我的软件及各种配置 1.建立了web工程,build path 使用的是默认的j2EE1.8(只有配 ...

  5. Go 嵌入类型

    文章转载地址:https://www.flysnow.org/2017/04/06/go-in-action-go-embedded-type.html 嵌入类型或嵌套类型,这是一种可以把已有类型的声 ...

  6. Linux——高效玩转命令行

    [0]统计文件or压缩文件的行数 zcat file.gz | sed -n '$='   #迅速.直接打印出多少行.-n 取消默认的输出,使用安静(silent)模式    '$='  不知道是什么 ...

  7. python中import与from方法总结

    一.模块&包简介 模块:所谓模块就是一个.py文件,用来存放变量,方法的文件,便于在其他python文件中导入(通过import或from). 包(package): 包是更大的组织单位,用来 ...

  8. poi 导入Excle

    一,AOP 是什么 Apache POI 提供java 程序对Microsoft Office格式文档的读写功能操作 二,所需要的jar包 三,实现代码 1, 读取Excle 返回Workbook格式 ...

  9. 【GO】【环境配置】

    1.首先下载GO的安装包:https://golang.org/doc/install#testing 找到上面一个Download超大按钮,找不到的可以不用再看了. 下载完成,安装. 安装成功后,启 ...

  10. php安全开发(1)文件包含漏洞

    开发过程总结的漏洞: 一,,如何造成包含漏洞:在通过函数包含文件时,由于没有对包含的文件名进行有效的过滤处理,被攻击者利用从而导致了包含了Web根目录以外的文件进来,就会导致文件信息的泄露甚至注入了恶 ...