21. Merge Two Sorted Lists[E]合并两个有序链表
题目
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]合并两个有序链表的更多相关文章
- LeetCode 21. Merge Two Sorted Lists(合并两个有序链表)
题意:合并两个有序链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...
- 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 ...
- [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 ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- merge two sorted lists, 合并两个有序序列
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Lis ...
- 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 ...
- 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 ...
- 23. Merge k Sorted Lists[H]合并k个排序链表
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 61.Merge k Sorted Lists(合并k个排序链表)
Level: Hard 题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and descri ...
随机推荐
- SQL Server2008优化之SET STATISTICS开关
一.准备工作 缓存对于某个查询的性能影响十分之大,所以优化之前要清空缓存. 清除Buffer Pool时面的所有缓存 DBCC DROPCLEANBUFFERS清除Buffer Pool里的所有缓存的 ...
- I2C controller core之Bit controller(01)
FPGA proven, AISC proven, I2C controller core from OpenCores http://opencores.org/project,i2c Bit-co ...
- CNN结构:用于检测的CNN结构进化-分离式方法
前言: 原文链接:基于CNN的目标检测发展过程 文章有大量修改,如有不适,请移步原文. 参考文章:图像的全局特征--用于目标检测 目标的检测和定位中一个很困难的问题是,如何从数以万计的候选 ...
- Currying vs Partial Application
柯里化相当于函数重构: 偏函数相当于函数适配. So, what is the difference between currying and partial application? As we s ...
- layui 下拉框取值
layui.use('form', function () { var form = layui.form; form.on('select(Status)', function (data) { c ...
- 【转载】Java 反射详解
目录 1.什么是反射? 2.反射能做什么? 3.反射的具体实现 4.根据反射获取父类属性 4.反射总结 反射反射,程序员的快乐! 1.什么是反射? Java反射就是在运行状态中,对于任意一个类,都能够 ...
- PAT_A1126#Eulerian Path
Source: PAT A1126 Eulerian Path (25 分) Description: In graph theory, an Eulerian path is a path in a ...
- OBS直播和相关操作
OBS Studio(Open Broadcaster Software)是一个免费的开源的视频录制和视频实时流软件.其有多种功能并广泛使用在视频采集,直播等领域. https://obsprojec ...
- php中的empty()方法
empty() 判断一个变量是否为“空”,isset() 判断一个变量是否已经设置.empty还会检测变量是否为空.为零.当一个变量值为0,empty() 认为这个变量同等于空,即相当于没有设置.例如 ...
- 转载 - KMP算法
出处:http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html KMP算法 在介绍KMP算法之前,先介绍一下BF算法. 一. ...