①英文题目

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 ms, 在所有 Java 提交中击败了98.26%的用户
          内存消耗 :39.3 MB, 在所有 Java 提交中击败了68.88%的用户

⑥学到的知识

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 (合并两个有序链表)(链表)的更多相关文章

  1. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  2. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  3. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

随机推荐

  1. Kubernetes的Ingress简单入门

    目录 一.什么是Ingress 二.部署Nginx Ingress Controller 三.部署一个Service将Nginx服务暴露出去 四.部署一个我们自己的服务Cafe 五.部署ingress ...

  2. spring-boot-dependencies、spring-boot-starter-parent、io.spring.platform详解

    上一篇文章介绍了springboot依赖版本号管理的几种方式,现在来详细分析一下spring-boot-dependencies.spring-boot-starter-parent.io.sprin ...

  3. opencv::凸包-Convex Hull

    概念介绍 什么是凸包(Convex Hull),在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部. 正式定义:包含点集合S中所有点的最小凸多边形称为凸包 Graham扫描算法 首先选 ...

  4. tinyxml2

    网上下载tinyxml2:tinyxml2.h和tinyxml2.cpp 加载xml XMLDocument doc;   doc.LoadFile("test.xml");   ...

  5. Libevent::evhttp服务器

    #include <cstdio> #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  6. Django RESRframework奇淫技巧

    Django RESRframework Mixins, ViewSet和router配合使用 Mixins的类共有五种 CreateModelMixin ListModelMixin Retriev ...

  7. C++bosst遍历文件目录,根据文件名返回文件路径。

    VS2071安装Boost库 安装boost库 接着安装boost_system-vc140(可根据开发需求,更改版本) 废话不多说,上代码 // 测试程序.cpp : 此文件包含 "mai ...

  8. Activity 学习(一) 插件安装篇

    目录 Ider下安装 Eclipse下安装 Ider安装图解 首先,创建一个普通的Java工程即可,然后按照下面流程进行: 1:点击菜单中的File(最左上角),选择settings 2:plugin ...

  9. Ubuntu18.04 安装谷歌BBR

    说明:Ubuntu 18.04前几天发布了,改变挺大的,内核也直接升到了正式版4.15,而BBR内核要求为4.9,也就是说满足了,所以我们不需要换内核就可以很快的开启BBR,这里简单说下方法. 提示: ...

  10. Spring中@Resource注解报错

    描述:Spring框架中,@Resource注解报错,在书写时没有自动提示 解决方法:因为maven配置文件的pom.xml文件中缺少javax.annotation的依赖,在pom.项目路中加入依赖 ...