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 ...
随机推荐
- WEB笔记-5、字体和文本
字体样式 font-family font-size font-weight font-style font-variant font(简写) 常用字体: serif,衬线字体,每个字符笔画末端会有装 ...
- 复习java第五天(枚举、Annotation(注释) 概述)
一.枚举 传统的方式: •在某些情况下,一个类的对象是有限而且固定的.例如季节类,只能有 4 个对象 •手动实现枚举类: —private 修饰构造器. —属性使用 private final 修饰. ...
- 时序分析:ARIMA模型(非平稳时间序列)
转载于一篇硕士论文.... ARIMA模型意为求和自回归滑动平均模型(IntergratedAut少regressive MovingAverageModel),简记为ARIMA(p,d,q),p,q ...
- 作业07之《MVC模式》
MVC(Model View Controller)模型-视图-控制器 MVC与模板概念的理解 MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的 ...
- 重新理解 Monad
对于大多数刚刚入门函数式编程的同学来说,monad(单子.又叫单体)可能是这里面的一道坎.你可能对 map . flatMap 以及 filter 再熟悉不过,可是到了高阶的抽象层次上就又会变得一脸懵 ...
- Android LinearLayout整个布局设置不可点击
1,activity的xml布局(布局中有个Button按钮,点击按钮弹出一个popupwindow ) <?xml version="1.0" encoding=" ...
- [luogu2679] 子串 (多维dp)
传送门 Description 有两个仅包含小写英文字母的字符串 A 和 B . 现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在字符串 A 中出现的顺序依次连接起来 ...
- [luogu2165 AHOI2009] 飞行棋 (枚举)
传送门 Description 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. Input ...
- 9.简单理解ES分布式
主要知识点: 1.Elasticsearch对复杂分布式机制的透明隐藏特性 2.Elasticsearch的垂直扩容与水平扩容 3.增减或减少节点时的数据rebalance 4.master节 ...
- poj 3177&&poj 3352加边构双联通(有重边)用tarjan 模板求的
#include<stdio.h>/* 求边双联通分量和求强连通差不多,先缩点求出叶子节点的个数 */ #include<string.h> #define N 5100 st ...