题目:

  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.

解决方案:276ms

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode newList = head; if(l1 == null || l2 == null){//两者有为空的情况
if(l1==null)
return l2;
if(l2 == null)
return l1;
} while(l1!=null && l2!=null){//两者都不为空 if(l1.val <= l2.val){
newList.next = l1;
l1 = l1.next;
}else{// if(l1.val < l2.val)
newList.next = l2;
l2 = l2.next;
}
newList = newList.next;
}
while(l1!=null){
newList.next = l1;
l1 = l1.next;
newList = newList.next;
}
while(l2!=null){
newList.next = l2;
l2 = l2.next;
newList = newList.next;
} return head.next;
}
}

总结:

  这道题目很简单,就是我们以前数据结构学习的时候的一道课本上的算法代码,其实思路就在那里,很容易就想到,但是我这里犯错了,我在第一个while中把nextList = nextList.next放到了while第一句里面,当然下面的代码也就是nextList = l1;这样了,但是这样是不对的,具体原因我也没弄清楚,希望有人帮我解决问题。多谢诸位。

贴错误代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode newList = head; if(l1 == null || l2 == null){//两者有为空的情况
if(l1==null)
return l2;
if(l2 == null)
return l1;
} while(l1!=null && l2!=null){//两者都不为空
newList = newList.next;
if(l1.val <= l2.val){
newList = l1;
l1 = l1.next;
}else{// if(l1.val < l2.val)
newList = l2;
l2 = l2.next;
} }
while(l1!=null){
newList.next = l1;
l1 = l1.next;
newList = newList.next;
}
while(l2!=null){
newList.next = l2;
l2 = l2.next;
newList = newList.next;
} return head.next;
}
}

代码提示错误:

Input:     {2}, {1}
Output: {}
Expected: {1,2}

纠结,这个问题都没搞懂。。。

leetcode 21.Merge Two Sorted Lists ,java的更多相关文章

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

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

  3. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

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

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

  6. # 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

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

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

  9. Leetcode 21. Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

随机推荐

  1. EF Code-First 学习之旅 EntityTypeConfiguration<TEntity>

    之前我们配置的实体都都在OnModelCreating方法中,如果有很多实体的话,OnModelCreating方法管理很麻烦 我们可以用单独的类来管理配置,继承EntityTypeConfigura ...

  2. Maven配置与创建

    1.下载Maven工具 从maven官网下载:https://maven.apache.org/download.cgi apache-maven-x.x.x-bin.zip ,解压到指定目录,例如D ...

  3. ubuntu 14.04 建立wifi热点

    昨天突然想起来我可以用笔记本搞一个热点这样我的手机就不用上流量了,但是手机死活搜不到建好的信号,目前的解决方案如下: 直接用ubuntu自带的创建wifi网络功能是不好使的,因为android系统不支 ...

  4. CommStringLib

    #include "syswatcher/CommStringLib.h" //#include "String16.h" #undef LOG_TAG #de ...

  5. RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素

    RemoveDuplicatesFromSortedArrayI: 问题描述:给定一个有序数组,去掉其中重复的元素.并返回新数组的长度.不能使用新的空间. [1,1,2,3] -> [1,2,3 ...

  6. 记升级mysql后的一次故障

    一.问题背景 接上级要求,某生产数据库需要实施备份:刚好漏洞扫描报告出来,mysql 版本需要升级到5.7.20,于是就未雨绸缪,先写脚本.脚本在mysql旧版本下完全可用(未升级前,mysql 为5 ...

  7. servlet的补充

    1 request client获得请求. response 设置client响应2 text/html13123 不明确的响应用下载处理.3 MIME类型不明确在w3shool4 两种写utf-8的 ...

  8. mysql数据库优化课程---9、php用什么写的

    mysql数据库优化课程---9.php用什么写的 一.总结 一句话总结:php是用c语言写的,所以php里面的那些模块什么都是c语言 c 1.php用什么写的? c php是用c语言写的,所以php ...

  9. Java 子类实例化对象的过程

    子类实例化是否会实例化父类? 不会.父类在子类实例化过程中是并没有被实例化,java中new子类没有实例化父类,只是调用父类的构造方法初始化了,子类从父类继承来的属性,这个调用是子类的对象调用的父类的 ...

  10. javascript 对象简单介绍(二)

    JavaScript Array(数组) 对象数组对象的作用是:使用单独的变量名来存储一系列的值. 什么是数组?数组对象是使用单独的变量名来存储一系列的值.如果你有一组数据(例如:车名字),存在单独变 ...