[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 splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
②中文题目
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
③思路
就是判断大小,注意,比着比着,l1或者l2就会空掉,然后导致空指针的问题,进而报错。
④代码
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode zf=null;
// ListNode curr=zf;
ListNode temp1=l1;
ListNode temp2=l2;
if(temp1==null&&temp2!=null){
zf=temp2;
temp2=temp2.next;
}
if(temp1!=null&&temp2==null){
zf=temp1;
temp1=temp1.next;
}
if(temp1!=null&&temp2!=null){
if(temp1.val<temp2.val){
zf=temp1;
temp1=temp1.next;
}
else{
zf=temp2;
temp2=temp2.next;
}
} //到此,zf是最小的结点,也是头部。
ListNode curr=zf;
while(temp1!=null||temp2!=null){
if(temp1==null&&temp2!=null){
curr.next=temp2;
temp2=temp2.next;
curr=curr.next;
}
if(temp2==null&&temp1!=null){
curr.next=temp1;
temp1=temp1.next;
curr=curr.next;
}
if(temp1!=null&&temp2!=null&&temp1.val<temp2.val){
curr.next=temp1;
temp1=temp1.next;
curr=curr.next;
}
if(temp1!=null&&temp2!=null&&temp1.val>=temp2.val){
curr.next=temp2;
temp2=temp2.next;
curr=curr.next;
}
}
return zf;
}
}
⑤运行结果
⑥学到的知识
1、如果超时,那去考虑是不是while()的括号里写的表达式,一直跳不出while循环。
2、DriverSolution__.__helper__这种报错,是空指针。一般就是某个链表已经到链尾了,代码还在找它的next,从而导致空指针。
⑦别人的答案
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
else if (l2 == null) {
return l1;
}
else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2); //递归
return l1; //因为在递归,所以此处可以返回return l1
}
else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
} }
}
⑧从别人代码学到的东西
1、 在我的代码里,为了最后return的时候,能从头结点开始return,我在我的代码的25行,给zf找了个复制体curr,让curr去做事,让zf一直表示头结点,最后return zf。
而在别人的代码里,因为第10行用了递归,所以第11行return个l1就行了。 ,体会下自己的想法和别人的差距。
[LC]21题 Merge Two Sorted Lists (合并两个有序链表)(链表)的更多相关文章
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- [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合并两个有序链表 (C++)
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- [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 ...
- 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 ...
- 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] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
随机推荐
- Java学习笔记之方法
前言:如果把所有代码都写到main方法中,后果是什么? 1,结构混乱 不清晰 2,不能重用 方法:规则:方法写在类中,不能写在其它方法中.方法不能嵌套方法 如何定义方法: 访问修饰符 返 ...
- .net工作流引擎ccflow开发平台属性功能的隐藏显示介绍
关键字: 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 表单引擎 工作流功能说明 工作流设计 工作流快速开发平台 业务流程管理 bpm工作流系统 java工 ...
- js中submit和button的区别
今天写一个js验证 遇到点小坑 记录一下 button-普通按钮,submit-提交按钮. submit是button的一个特例,也是button的一种,它把提交这个动作自动集成了,submit和bu ...
- SQL Server Try Catch 异常捕捉
SQL Server Try Catch 异常捕捉 背景 今天遇到一个关于try catch 使用比较有意思的问题.如下一段代码: SELECT @@TRANCOUNT AS A BEGIN TRY ...
- USACO环绕岛屿Surround the Islands 并查集 枚举暴力
题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...
- 【RabbitMQ 实战指南】一 延迟队列
1.什么是延迟队列 延迟队列中存储延迟消息,延迟消息是指当消息被发送到队列中不会立即消费,而是等待一段时间后再消费该消息. 延迟队列很多应用场景,一个典型的应用场景是订单未支付超时取消,用户下单之后3 ...
- Nginx 了解一下?
这篇文章主要简单的介绍下 Nginx 的相关知识,主要包括以下几部分内容: Nginx 适用于哪些场景? 为什么会出现 Nginx? Nginx 优点 Nginx 的编译与配置 Nginx 适用于哪些 ...
- 一文读懂Java类加载机制
Java 类加载机制 Java 类加载机制详解. @pdai Java 类加载机制 类的生命周期 类的加载:查找并加载类的二进制数据 连接 验证:确保被加载的类的正确性 准备:为类的静态变量分配内存, ...
- Codeblocks 等软件 修改源代码后 不能立即执行的解决办法||exe文件删除慢
不懈地奋斗了两天,终于找到原因了. 记录如下 症状: Codeblocks .Visual Studio 都出现此问题:修改源代码 无法立即执行 ,就是:cannot open output file ...
- 不想用锐捷怎么办?锐捷出问题|锐捷不能用怎么办?用menohust代替吧
首先获取 MentoHUST(代替锐捷网络认证客户端) V4.1.0.2001 绿色免费版 解压到任意目录 用管理员身份 启动 安装&卸载 .bat(右键用管理员运行) 这个文件可能乱码了 ...