[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. 这 ...
随机推荐
- Lombok中关于@Data的使用
当你在使用 Lombok 的 @Data 注解时,其实会有一些坑需要关注,今天就让我们来见识一下. Lombok 先来简单介绍一下 Lombok ,其官方介绍如下: Project Lombok ma ...
- [Luogu2967] 视频游戏的麻烦Video Game Troubles
农夫约翰的奶牛们游戏成瘾!本来约翰是想要按照调教兽的做法拿她们去电击戒瘾的,可是 后来他发现奶牛们玩游戏之后比原先产更多的奶.很明显,这是因为满足的牛会产更多的奶. 但是,奶牛们在哪个才是最好的游 ...
- [Luogu2879][USACO07JAN]区间统计Tallest Cow
题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a p ...
- RIDE-工程、测试套件、测试用例三者关系
理论 type的选择: 一般来说:测试项目(directory)-测试套件(file)-测试用例 本质上,“测试项目”和“测试套件”并没有什么区别,但是testcase只能放在file类型的test ...
- python类中的self
class User: def walk(self): print(self,"正在慢慢走") # User.walk() # 会报错 TypeError: walk() miss ...
- python 可变数量参数 ( 多参数返回求 参数个数,最大值,最大值)
一. 自定义一串数字求 参数个数,最大值,最大值()---------方法一: def max(*a): m=a[0] p=a[0] n=0 for x in a: if x>m: m=x n+ ...
- python函数与异常处理
一.python函数 1.函数自定义格式: 分为有无返回值两种类型 def 函数名(): 代码语句 -------- -------- return 参数1,(参数2等)--------------- ...
- 你必须知道的容器监控 (2) cAdvisor
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章.上一篇我们了解了docker自带的监控子命令以及开源监控工具Weave Scop ...
- fenby C语言 P33
#include <stdio.h> int main(void){ char *ps="my dream is to be a programmer"; printf ...
- SpringBoot整合MybatisPlus3.X之SQL执行分析插件(十四)
pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...