①英文题目

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. Struts2:搭建原理

    记录下,struts2的搭建过程: 1核心jar包: struts-2.1.8\apps\struts2-blank-2.1.8.war 解压后 在struts2-blank-2.1.8\WEB-IN ...

  2. [BZOJ1076] 奖励关

    Description 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物, 每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的 ...

  3. libevent::bufferevent

    #include <cstdio> #include <netinet/in.h> #include <sys/socket.h> #include <fcn ...

  4. Mysql面试题及千万级数据查询优化

    今天在说Mysql查询优化之前,我先说一个常见的面试题,并带着问题深入探讨研究.这样会让大家有更深入的理解. 一,Mysql数据库中一个表里有一千多万条数据,怎么快速的查出第900万条后的100条数据 ...

  5. 微信小程序--获取用户地理位置名称(无须用户授权)的方法

    准备 1.在http://lbs.qq.com/网站申请key 2.在微信小程序后台把apis.map.qq.com添加进request合法域名 效果 添加封装 /** * 发起网络请求 * @par ...

  6. human_pose_estimation_demo的进一步研究

    一.demo能力 OpenVINO提供了范例(human_pose_estimation_demo),能够在CPU上以较快速度识别出多人 -iE:/OpenVINO_modelZoo/head-pos ...

  7. Java socket Tcp协议 实现文件传输

    1.文件加密上传后发现文件已损坏: 原因:使用 read(byte[]) 方法不能够准确的获取到正确的字节数,有可能比 byte[].length 小,所以在解密的时候出现错误. 解决办法: 判断读取 ...

  8. unity 基于scrollRect实现翻页显示

    unity 基于scrollRect实现翻页显示,并定为到某一页,而不是某一页的中间方法(第二个脚本采用实际位置计算,并在update里实现平滑过渡): 组场景时,经常需要获取鼠标(或者点击)开始结束 ...

  9. 2018.8.8 python 初识函数

    主要内容: 1.函数定义 2.函数名,函数体以及函数的调用 3.函数的返回值 4.函数的参数 一.函数的定义 函数:对代码块和功能的封装和定义. 二.函数名.函数体及函数的调用 我们使用def关键字来 ...

  10. Linux Cannot allocate memory问题

    查找了一下相关文档,发现这个错误的含义其实就是像它自己说的,没法分配内存了. The problem is inherent with the way Java allocates memory wh ...