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 ...
随机推荐
- version_compare ()
version_compare() ----对比两个「PHP 规范化」的版本数字字符串 version_compare ( string $version1 , string $version2 [, ...
- python科学计算库-pandas
------------恢复内容开始------------ 1.基本概念 在数据分析工作中,Pandas 的使用频率是很高的, 一方面是因为 Pandas 提供的基础数据结构 DataFrame 与 ...
- java虚拟机规范学习笔记之数据类型
1.1 class文件格式 编译后被Java虚拟机所执行的代码使用了一种平台中立的二进制格式来表示,并且经常以文件的形式来存储,这种格式称为class文件格式.class文件格式中精确的定义了类与接口 ...
- 第02组 Beta冲刺(3/5)
队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 数据库实践的报告 提交记录(全组共用) 接下来的计划 加快校园百科的进度 还剩下哪些任务 学习软工的理论课 学习代码评估. ...
- 物联网架构成长之路(39)-Bladex开发框架环境搭建
0.前言 上一篇博客已经介绍了,阶段性小结.目前第一版的物联网平台已经趋于完成.框架基本不变了,剩下就是调整一些UI,还有配合硬件和市场那边,看看怎么推广这个平台.能不能挣点外快.第一版系统虽然简陋, ...
- 【shell脚本】定时备份日志===logBackup.sh
定时备份日志 设置执行权限 [root@VM_0_10_centos shellScript]# chmod a+x logBackup,sh 脚本内容 [root@VM_0_10_centos sh ...
- MSM8909中LK阶段LCM屏适配与显示流程分析(一)
1.前言 在驱动开发中,我们往往需要适配一些新的屏幕或者调试一些屏幕的参数等,对于Qualcomm的MSM8909这款SoC,当启动Android系统时,会有一个LK阶段,该阶段用来启动Linux内核 ...
- Knative 应用在阿里云容器服务上的最佳实践
作者|元毅 阿里云智能事业群高级开发工程师 相信通过前面几个章节的内容,大家对 Knative 有了初步的体感,那么在云原生时代如何在云上玩转 Knative?本篇内容就给你带来了 Knative 应 ...
- 超详细Pycharm部署项目视频教程
在实际的工作中,不管你是开发.测试还是运维人员,都应该掌握的一项技能就是部署项目,简单说就是把项目放到服务器中,使其正常运行.今天猪哥就以咱们的微信机器人项目为例子,带大家来部署一下项目.本文将会详细 ...
- 【MySQL】多表查询&事务&权限管理
多表查询: 查询语法: select 列名列表 from 表名列表 where.... 例子: 创建部门表 CREATE TABLE dept( id INT PRIMARY KEY AUTO_INC ...