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 ...
随机推荐
- 【oracle开发】wmsys.wm_concat介绍
wmsys.wm_concat是一个聚合函数,其作用是将一列数据转换成一行,也就是我们常用的行专列,但是该函数是一个undocument函数,所以不推荐大家使用这个函数.因为在后续的版本中还提不提供这 ...
- Assembly之instruction之CMP
CMP[.W] Compare source and destinationCMP.B Compare source and destination Syntax CMP src,dst or ...
- 【MySQL】ERROR 1005: Can't create table (errno: 150)的错误解决办法
在mysql 中建立引用约束的时候会出现MySQL ERROR 1005: Can't create table (errno: 150)的错误信息结果是不能建立 引用约束. 出现问题的大致情况 1. ...
- js-构造数组
js中,字符串的特性跟数组非常类似.数组是一种很重要的数据结构.在java中,数组声明的时候就要为其指定类型,数组中只能放同一种类型的数据.Js中的数组可以放不同的类型,但是是有序的,类似于java中 ...
- github+hexo(window10)
一.申请github账户 二.先安装node.js.git 本地: 三.安装hexo(建立静态网页,用Markdown写博客) 1.创建文件地址 在合适的地方新建一个文件夹,用来存放自己的博客文件,比 ...
- lunix下的redis数据库操作——zset有序集合
创建:(有序集合存在一个权重的概念) zadd zset 1 a 2 b 3 c 4 d 5 e 6 f 7 g # 输出: # 1) "a" # 2) "b" ...
- lucene_04_解析语法查询
解析语法查询就是调用方法查询的原始查询 例如: 查询所有的查询器的语法为:*:*,因为lucene查询是根据term来做的,既是:key:value类型.*:*表示所有域中的所有值. api调用语法解 ...
- 重命名文件及html
import os import nltk from bs4 import BeautifulSoup as bs def get_txt_name_from_bak_name(bak_name): ...
- NodeJS的安装与使用
Node.js 就是运行在服务端的 JavaScript.越来越多的人在使用它,通过他我们可以用JavaScript来构建后台.对于前端程序员而言,不言而喻这是一条多么令人振奋的消息.对于后台程序员而 ...
- IE-FSC
Top3: Top2: FSC related to Redis: (Redis = https://www.cnblogs.com/ngtest/p/10693750.html) FSC statu ...