题目内容: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. 编译openwrt时报错:g++: internal compiler error: Killed (program cc1plus)

    答: 这是内存不足导致的,增大内存或者减少运行的线程即可

  2. Unity 2018.2.8 旧版本安装包和破解软件

    声明:本文所提供的所有软件均来自于互联网,仅供个人研究和学习使用,请勿用于商业用途,下载后请于24小时内删除,请支持正版! 最近Unity官网下载的旧版本,都无法正常破解.此链接有之前下载的离线安装包 ...

  3. 蓝牙 - 小米手环3 NFC版BLE协议研究

    0x01 前言 最近买到了小米手环3nfc版本,基本上实现了我对手环的所有功能需求,高中的时候就缠线圈做过戒指一卡通,但是缺陷是不好看,而且只能储存一张卡,等 手环3nfc版我认为比较好的功能 可以储 ...

  4. 【Core】当前 .NET SDK 不支持将 .NET Core 2.2 设置为目标。请将 .NET Core 2.1 或更低版本设置

    问题起因: 新的电脑,打开core2.2的项目时,因为没有安装2.2 sdk,项目编译失败 所以在选择目标框架下拉框选择安装其他目标框架 会跳转到官网下载sdk:https://dotnet.micr ...

  5. Pandas-数据的合并与拼接

    Pandas包的merge.join.concat方法可以完成数据的合并和拼接,merge方法主要基于两个dataframe的共同列进行合并,join方法主要基于两个dataframe的索引进行合并, ...

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

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

  7. Go语言学习之14 商品秒杀架构设计与开发

    本节主要内容 1. 秒杀抢购背景2. 秒杀抢购架构设计&模块划分3. 秒杀抢购接入层实现 1. 秒杀抢购背景 (1)架构分析 电商网站架构 秒杀抢购1.0 (2)上述网站架构问题 和已有电商逻 ...

  8. GT sport赛道详解 - Dragon Trail | 龙之径

    参考:GT sport所有赛道简介 今天的心情变化挺大,从绝望放弃到豁然开朗. 前言:GT sport有个排位赛,是每位sim赛车手提升自己等级的唯一途径,其中一个排位赛就是龙之径II(逆时针跑),我 ...

  9. 下载了好久的IntelliJ IDEA一直没用

    今天想试一下然后打开了IJ,发现我居然一直没有配置JDK macos 配置完全按照这个一步步走下去就好了 https://jingyan.baidu.com/album/597a0643336e263 ...

  10. Lua 用指定字符或字符串分割输入字符串,返回包含分割结果的数组

    // 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 // @function [parent=#string] split // @param string input 输入字符串 // ...