LeetCode21 Merge Two Sorted Lists
题意:
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. (Easy)
分析:
链表题,注意细节即可。
链表merge和数组merge的不同在于链表不用拷一份出来,倒腾一下指针就可以啦。
注意事项有:
1. dummy node用于输出,head(或者换个名字)用于遍历;
2. l1,l2是不是为空,一个结束遍历之后记得把另一个剩下的加上去。
代码:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == nullptr) {
return l2;
}
if (l2 == nullptr) {
return l1;
}
ListNode dummy();
ListNode* head = &dummy;
while (l1 != nullptr && l2 != nullptr) {
if (l1 -> val < l2 -> val) {
head -> next = l1;
head = head -> next;
l1 = l1 -> next;
}
else {
head -> next = l2;
head = head -> next;
l2 = l2 -> next;
}
}
if (l1 != nullptr) {
head -> next = l1;
}
if (l2 != nullptr) {
head -> next = l2;
}
return dummy.next;
}
};
优化一下:
head = head -> next是不论if 还是else都要做的,拿出来写;
按本题的写法和ListNode的定义,其实开始不用判断l1,l2是否为空。(但一般来讲还是写上为好...)
代码:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode dummy();
ListNode* head = &dummy;
while (l1 != nullptr && l2 != nullptr) {
if (l1 -> val < l2 -> val) {
head -> next = l1;
l1 = l1 -> next;
}
else {
head -> next = l2;
l2 = l2 -> next;
}
head = head -> next;
}
if (l1 != nullptr) {
head -> next = l1;
}
if (l2 != nullptr) {
head -> next = l2;
}
return dummy.next;
}
};
今天七夕,算是半个假期,状态不是很好,水一道链表题练练手啦。明天开始重新步入正轨按照题号刷啦!!!
LeetCode21 Merge Two Sorted Lists的更多相关文章
- LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)
21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] 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 ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- 71. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge Two Sorted Lists
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...
随机推荐
- PAT甲级——A1053 Path of Equal Weight
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- opencv读取的彩色图像,数据是GBR而不是RGB
开发久了,容易想当然 直到数据怎么也不对的时候,才想起来查一下手册 三个像素,当然没有这么大的像素,这是放大之后的 数据输出
- 【DM642】H.264源代码在DM642上的移植
TI公司提供了用于C语言开发的CCS(Code Composer Studio),该平台包括了优化的ANSI编译器,使之可以使用C语言开发DSP程序.这种方法不仅使DSP开发的速度大大加快,而且DSP ...
- Python Flask高级编程之从0到1开发《鱼书》精品项目
Python Flask高级编程之从0到1开发<鱼书>精品项目 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感 ...
- Hibernate-一对多|多对一-多对多
1 一对多|多对一 1.1 关系表达 表中的表达 表中的表达 实体中的表达 orm元数据中表达 一对多 多对一 1.2 操作 操作关联属性 1.3 进阶操作 级联操作 结论: 简化操作.一定要用,s ...
- vim编辑器操作①
Linux文本编辑器: 行编辑器:sed 全屏编辑器:nano,vi/vim 本文主要介绍说明vim编辑器的相关使用: 其有三种模式,即: 编辑模式(默认模式).插入模式(输入模式).末行模式(内置的 ...
- MySQL 普通注册插入优化。
普通做法是: 用户通过手机号注册.默认是根据这个手机号去用户表里查询,看有没有这个手机号,有那么就提示已注册.否则就执行注册插入数据库操作.这里其实正常注册流程是两次数据库操作的(查询,插入): 优化 ...
- SPOJ 2916 GSS5 - Can you answer these queries V
传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...
- 洛谷P1062 数列 [2017年6月计划 数论03]
P1062 数列 题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13,… ...
- chmod 777 修改权限之后,文件夹颜色变绿:解决方案
修改前: 方法一: ls --color=none #不显示颜色 方法一修改后: 方法二:修改配色 ①安装git +++可用在windows下载之后用ftp传上去:http://pan.baidu.c ...