leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述:
合并两个有序链表。
解决:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1)
return l2;
if (!l2)
return l1;
if (!l1 && !l2)
return NULL;
ListNode* ret = new ListNode();
ListNode* now = ret;
while (l1 || l2) {
if (!l1) {
now->next = l2;
break;
}
else if (!l2) {
now->next = l1;
break;
}
ListNode** min = l1->val <= l2->val?&l1:&l2;
now->next = *min;
auto save = (*min)->next;
(*min)->next = NULL;
*min = save;
now = now->next;
}
return ret->next;
}
leetcode 21 Merge Two Sorted Lists 合并两个有序链表的更多相关文章
- 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 splici ...
- [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 ...
随机推荐
- bacnet ip转MQTT
迈思德网关最新支持BACNET IP协议,可以将BACNET IP转换为MODBUS.MQTT.OPC等协议,与百度天工,阿里云等无缝对接. 支持AI.AO.DI.DO.AV.DV六个对象的读写.
- CS231n课程笔记翻译6:神经网络笔记 part1
译者注:本文智能单元首发,译自斯坦福CS231n课程笔记Neural Nets notes 1,课程教师Andrej Karpathy授权翻译.本篇教程由杜客翻译完成,巩子嘉和堃堃进行校对修改.译文含 ...
- TypeScript学习笔记(六) - 模块
本篇将介绍TypeScript里的模块,和使用方法. 在ECMAScript 2015标准里,JavaScript新增了模块的概念.TypeScript也沿用了这个概念. 一.模块的导入和导出 模块在 ...
- Oracle Database express 11g 第 2 版安装和配置
官方Oracle Database 快捷版 11g 第 2 版的下载地址: http://www.oracle.com/technetwork/cn/products/express-edition/ ...
- oracle 脚本创建数据库的相关文章,教程,源码
学步园推荐专题: 关于oracle 脚本创建数据库的相关文章 文章标题 文章链接 文章简介 oracle命令行创建数据库的示例脚本 http://www.xuebuyuan.com/964527.ht ...
- FastAdmin 插件配置文件 info.ini 中的 state 什么意思?
FastAdmin 插件配置文件 info.ini 中的 state 什么意思? 在插件配置中有一个 state ,这是配置插件开关的.
- FastAdmin 学习线路 (2018-06-09 更新)
FastAdmin 学习线路 以下为常规线路,非常规可跳过. FastAdmin 学习线路 基础 HTML CSS DIV Javascript 基础 jQuery php 基础 对象 命名空间 进阶 ...
- appium的三种等待方式 (还没实践过,记录在此)
参考:https://testerhome.com/topics/2576
- HDU 4762 Cut the Cake(高精度)
Cut the Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- app crawler1
app crawler简介 执行 java -jar appcrawler-2.1.3.jar 查看相关参数 -a, --app Android或者iOS的文件地址, 可以是网络地址, 赋值给appi ...