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的合并链表

图1:两个链表

图2:第一次递归

图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 ...
随机推荐
- 菜鸟使用 centOS 安装 redis 并放入service 启动 记录
1.下载redis: wget http://download.redis.io/releases/redis-2.8.17.tar.gz 若wget 不可用,请先安装wget yum install ...
- windows常用的cmd命令和常用操作。
这几日部署Jenkins,牵扯到很多东西,比如用到许多cmd命令和Linux命令.查找比较花时间,因此将查看的文档留下,以避免下次重新查找浪费时间. Windows cmd命令: http://blo ...
- C# ref 和 out 的使用
private void button1_Click(object sender, EventArgs e) { ; ; Fun(ref a,ref b); //把a的地址和b的地址 传递过去 Mes ...
- springboot 的 @Async
/** * Created by zhiqi.shao on 2018/4/3. */ @EnableAsync @Configuration public class TaskPoolConfig ...
- java实现简单窗体小游戏----球球大作战
java实现简单窗体小游戏----球球大作战需求分析1.分析小球的属性: 坐标.大小.颜色.方向.速度 2.抽象类:Ball 设计类:BallMain—创建窗体 BallJPanel—画小 ...
- vue-属性传值 props
props属性传值 1.传具体的值 string(字符串) number(数值) boolean(布尔) 2.传一个引用 array(数组) object(对象) ----传引用----- 代码 ...
- Lua操作系统库、流、文件库
Lua操作系统库.流.文件库 1.Lua中所有的操作系统库函数 (1)os.clock() --功能:返回执行该程序cpu花费的时钟秒数 (2)os.time(...) --按参数的内容返回一个时间值 ...
- [USACO10DEC]宝箱Treasure Chest
区间DP,但是卡空间. n2的就是f[i,j]=sum[i,j]-min(f[i+1][j],f[i][j-1])表示这个区间和减去对手取走的最多的. 但是空间是64MB,就很难受 发现一定是由大区间 ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- 【hihocoder 1298】 数论五·欧拉函数
[题目链接]:http://hihocoder.com/problemset/problem/1298 [题意] [题解] 用欧拉筛法; 能够同时求出1..MAX当中的所有质数和所有数的欧拉函数的值; ...