[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. 这 ...
随机推荐
- 使用Docker搭建apache环境
Docker搭建apache环境 前言 操作机:ubuntu16 x64 Dockers servion 18.09.7 下载镜像 使用docker pull 拉取最新的 apache镜像 命令:do ...
- std::lock_guard 与 std::unique_lock
std::lock_guard 与 std::unique_lock 对 mutex 进行自动加解锁. mutex m; void fun() { unique_lock<mutex> m ...
- 关于vue使用的一些小经验
这一年来说,vue的势头很猛,用户量“噌”“噌”“噌”的涨 为了不掉队不落伍.在后台大哥的胁迫下,不得不选择用了它 刚开始很难接受vue的写法,在编辑器里很容易报错,基本上每行都会出现红色的波浪线 这 ...
- python类中的self
class User: def walk(self): print(self,"正在慢慢走") # User.walk() # 会报错 TypeError: walk() miss ...
- linux上war包方式安装Jenkins
我的安装环境:jdk1.8, linux系统为: [root@ipha-dev71-1 nmon]# cat /etc/redhat-release # Linux查看版本当前操作系统发行版信息 Ce ...
- 玩转u8g2 OLED库,一篇就够(分篇)
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- 介绍Webflux
介绍Webflux 关于WebFlux 我们知道传统的Web框架,比如说:struts2,springmvc等都是基于Servlet API与Servlet容器基础之上运行的,在Servlet3.1之 ...
- Hystrix dashboard - Unable to connect to Command Metric Stream.
在使用boot 2.0.*以上版本 + cloud Finchley.RELEASE 查看仪表盘的时候会报错 Unable to connect to Command Metric Stream &l ...
- 【构建工具】《Maven实战》读书笔记
Maven是我们在做Java开发过程中用经常用到的一个辅助工具.本篇博客是我学习Maven的一个记录博客,学习过程主要参考<Maven实战>这本书.同时也参考了Maven的官方文档. 1. ...
- arango集群部署
arango集群部署 ############arango集群操作################## arangodb3-3.3.16-1.x86_64.rpm(使用rpm包方式安装) arango ...