LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)
题目:
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
分析:
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
创建一个新的节点head,比较了l1和l2的值的大小,将较小的加入到head,依次比较,如果l1,l2有剩余,就接在后面即可。
l1 l2 head,p
↓ ↓ ↓
1->2->4 1->3->4 0
l1 l2 head p
↓ ↓ ↓ ↓
2->4 1->3->4 0->1
l1 l2 head p
↓ ↓ ↓ ↓
2->4 3->4 0->1->1
l1 l2 head p
↓ ↓ ↓ ↓
4 3->4 0->1->1->2
l1 l2 head p
↓ ↓ ↓ ↓
4 4 0->1->1->2->3
l1 l2 head p
↓ ↓ ↓ ↓
null 4 0->1->1->2->3->4
l1 l2 head p
↓ ↓ ↓ ↓
null null 0->1->1->2->3->4->4
最后返回head.next即可。
还可以递归求解此问题。
mergeTwoLists( l1, l2) //l1:1->2->4,l2:1->3->4
=>{1} + mergeTwoLists( l1, l2) //l1:2->4,l2:1->3->4
=>{1->1} + mergeTwoLists( l1, l2) //l1:2->4,l2:3->4
=>{1->1->2} + mergeTwoLists( l1, l2) //l1:4,l2:3->4
=>{1->1->2->3} + mergeTwoLists( l1, l2) //l1:4,l2:4
=>{1->1->2->3->4} + mergeTwoLists( l1, l2) //l1,l2:4
=>{1->1->2->3->4->4}
程序:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode head();
ListNode* p = &head;
while(l1 && l2){
if(l1->val < l2->val){
p->next = l1;
l1 = l1->next;
}
else{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
if(l1) p->next = l1;
if(l2) p->next = l2;
return head.next;
}
};
//递归
class Solution {
public:
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(l1, l2->next);
return l2;
}
}
};
LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)的更多相关文章
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- [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 ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- [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 ...
- 021 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 ...
- [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 ...
随机推荐
- Java常识2
JDK 的下载 安装 下载 官网 github安装 傻瓜式安装 JDK .JRE 注意问题 安装软件的 路径不能包含中文 空格 path环境变量 windows操作系统执行命令是所要搜寻的路径为什么要 ...
- poi实现excel数据的导入和导出
内容来源于网络,侵删. 1.需要的jar包 <dependency> <groupId>org.apache.poi</groupId> <artifactI ...
- 项目倒入maven 遇到的问题只有 main 了
归根结底是倒入错了: (1)首先 view->Toolbar; (2) 点击 File==>project structure 然后:在 project settings中点击 modu ...
- CodeForce 192D Demonstration
In the capital city of Berland, Bertown, demonstrations are against the recent election of the King ...
- 什么是JavaBean?
什么是JavaBean? 首先明确的是JavaBean是一种Java类,而且是一种特殊的.可重用的类. 必须具有无参数的构造器,所有的属性都是private的,通过提供setter和getter方法来 ...
- 使用wireshark学习TCP
TCP标志位: 在TCP传输中,标志位用于表示特定的连接状态或提供额外信息.每个标志位占用1比特.常用的TCP标志位包含以下几种: SYN Synchronous,TCP三次握手建立连接的第一步,主动 ...
- LINQ 之 LookUp
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 本文作者文采欠佳,文字表达等方面不是很好,但实际的代码例子是非常实用的,请作参考. 一.先准备要使用的类: 1.Person类: cl ...
- Pandas 学习 第9篇:DataFrame - 数据的输入输出
常用的数据存储介质是数据库和csv文件,pandas模块包含了相应的API对数据进行输入和输出: 对于格式化的平面文件:read_table() 对于csv文件:read_csv().to_csv() ...
- 【机器学习笔记】ID3构建决策树
好多算法之类的,看理论描述,让人似懂非懂,代码走一走,现象就了然了. 引: from sklearn import tree names = ['size', 'scale', 'fruit', 'b ...
- DataTable 转List
忘了出处 ,这个是转别人的 public class DataToList<T> where T : new() { /// <summary> /// 利用反射和泛型 / ...