题目

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.

Example:

 Input:1->2->4, 1->3->4

 Output:1->1->2->3->4->4


思路

思路一:建立一个新链表

建立一个新的链表,用来保存合并的结果。为了方便链表的插入,定义一个临时节点,否则每次都要遍历到节点的尾部进行插入。

思路二:递归法

  • 递归的终止条件:

    其中一个链表为空
  • 递归
    • 如果l1->val <= l2->val,则将l1的下一个节点记为l1->next与l2的合并链表
    • 如果l1->val > l2->val,则将l2的下一个节点记为l2->next与l1的合并链表

![](https://i.loli.net/2019/05/22/5ce5063f7e5f739567.jpg)
图1:两个链表
![](https://i.loli.net/2019/05/22/5ce506510660213051.jpg)
图2:第一次递归
![](https://i.loli.net/2019/05/22/5ce50666dbb1b64155.jpg)
图3:第二次递归


C++

  • 思路一
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {

        if(l1 == nullptr)
return l2;
if(l2 == nullptr)
return l1; ListNode* result = new ListNode(0) ; ListNode* temp = result; while(l1 != nullptr && l2 != nullptr){ if(l1->val <= l2->val){
temp ->next = l1;
l1 = l1 -> next;
}
else{
temp -> next =l2;
l2 = l2 ->next;
}
temp = temp ->next;
} if(l1 == nullptr)
temp -> next = l2;
else
temp -> next =l1; return result -> next;
}
  • 思路二
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {

        if(l1 == nullptr)
return l2;
if(l2 == nullptr)
return l1; if(l1->val <= l2->val){
l1->next = mergeTwoLists(l1->next ,l2);
return l1;
}
else{
l2->next = mergeTwoLists(l2->next ,l1);
return l2;
}
}

Python

21. Merge Two Sorted Lists[E]合并两个有序链表的更多相关文章

  1. LeetCode 21. Merge Two Sorted Lists(合并两个有序链表)

    题意:合并两个有序链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...

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

  3. [LC]21题 Merge Two Sorted Lists (合并两个有序链表)(链表)

    ①英文题目 Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...

  4. LeetCode 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  5. merge two sorted lists, 合并两个有序序列

    /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Lis ...

  6. 21. Merge Two Sorted Lists (Java 合并有序链表 空间复杂度O(1))

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  7. LeetCode OJ: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. 23. Merge k Sorted Lists[H]合并k个排序链表

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  9. 61.Merge k Sorted Lists(合并k个排序链表)

    Level:   Hard 题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and descri ...

随机推荐

  1. SQL Server2008优化之SET STATISTICS开关

    一.准备工作 缓存对于某个查询的性能影响十分之大,所以优化之前要清空缓存. 清除Buffer Pool时面的所有缓存 DBCC DROPCLEANBUFFERS清除Buffer Pool里的所有缓存的 ...

  2. I2C controller core之Bit controller(01)

    FPGA proven, AISC proven, I2C controller core from OpenCores http://opencores.org/project,i2c Bit-co ...

  3. CNN结构:用于检测的CNN结构进化-分离式方法

    前言: 原文链接:基于CNN的目标检测发展过程       文章有大量修改,如有不适,请移步原文. 参考文章:图像的全局特征--用于目标检测 目标的检测和定位中一个很困难的问题是,如何从数以万计的候选 ...

  4. Currying vs Partial Application

    柯里化相当于函数重构: 偏函数相当于函数适配. So, what is the difference between currying and partial application? As we s ...

  5. layui 下拉框取值

    layui.use('form', function () { var form = layui.form; form.on('select(Status)', function (data) { c ...

  6. 【转载】Java 反射详解

    目录 1.什么是反射? 2.反射能做什么? 3.反射的具体实现 4.根据反射获取父类属性 4.反射总结 反射反射,程序员的快乐! 1.什么是反射? Java反射就是在运行状态中,对于任意一个类,都能够 ...

  7. PAT_A1126#Eulerian Path

    Source: PAT A1126 Eulerian Path (25 分) Description: In graph theory, an Eulerian path is a path in a ...

  8. OBS直播和相关操作

    OBS Studio(Open Broadcaster Software)是一个免费的开源的视频录制和视频实时流软件.其有多种功能并广泛使用在视频采集,直播等领域. https://obsprojec ...

  9. php中的empty()方法

    empty() 判断一个变量是否为“空”,isset() 判断一个变量是否已经设置.empty还会检测变量是否为空.为零.当一个变量值为0,empty() 认为这个变量同等于空,即相当于没有设置.例如 ...

  10. 转载 - KMP算法

    出处:http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html KMP算法 在介绍KMP算法之前,先介绍一下BF算法. 一. ...