题目内容: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. 关于截取URL地址参数的方法

    JS获取URL中最后一个斜杠前面的内容 var url = window.location.href; var index = url.lastIndexOf("\/"); str ...

  2. 配置spring cache RedisCacheManager的序列化方法

    通过查看autoconfigure源码 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration; 部分源码如下: pr ...

  3. CF Round #551 (Div. 2) D

    CF Round #551 (Div. 2) D 链接 https://codeforces.com/contest/1153/problem/D 思路 不考虑赋值和贪心,考虑排名. 设\(dp_i\ ...

  4. Python装饰器的另类用法

    之前有比较系统介绍过Python的装饰器(请查阅<详解Python装饰器>),本文算是一个补充.今天我们一起探讨一下装饰器的另类用法. 语法回顾 开始之前我们再将Python装饰器的语法回 ...

  5. Vue-admin工作整理(十七):Mock模拟Ajax请求

    思路:使用Mock拦截actions请求,通过 Mock.mock(/\/getUserInfo/, 'post', getUserInfo) 进行拦截标示,然后将内容返回 export const ...

  6. ArcGIS Pro开发Web3D应用(3)——Server/Portal授权服务开发

    1.整体环境搭建完成 WebAdaptor.DataStore.Portal for arcgis.arcgis server.arcgis pro都成功部署安装,不管是同服务器还是不同服务器,最好做 ...

  7. js实现bind方法

    //目标函数 function fun(...args) { console.log(this); console.log(args); } //目标函数原型对象上的一个方法cher func.pro ...

  8. 如何增加亚马逊listing多个类目节点

    流量是电商销售的必要因素,可以说,任何成功的电商平台都离不开流量.亚马逊listing优化做得好,不仅能提高产品的曝光率,还能提升转换率,而好的类目可以吸引大的流量.帮你快速爬升. 首先我们来了解一下 ...

  9. Abstract与Virtual

    转自 http://www.cnblogs.com/wang7/archive/2012/04/17/2453624.html virtual和abstract都是用来修饰父类的,通过覆盖父类的定义, ...

  10. docker 中安装 rabbitMQ

    安装rabbitMQ的命令 docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=admin -e RAB ...