leetcode 21.Merge Two Sorted Lists ,java
题目:
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的更多相关文章
- [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 混合插入有序链表
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 (Easy)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
- 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 ...
- 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 [Difficulty: Easy]
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
- 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 解题思路
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(easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- IDEA 修改JSP和后端数据后,页面刷新可以实时更新
情况:刚开始使用IDEA进行开发时,发现修改JSP页面或者后端数据后,再刷新浏览器页面,发现没有变化,页面无更新. 这样就导致不得不频繁重启tomcat服务器.非常麻烦 解决方法: 步骤1. 先设置t ...
- Flume-NG源码阅读之FileChannel
FileChannel是flume一个非常重要的channel组件,非常常用.这个channel非常复杂,涉及的文件更多涉及三个包:org.apache.flume.channel.file.org. ...
- AOP切面用于系统日志
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework. ...
- JNIjw02
1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw02.h" JNIEXPORT void JNICALL ...
- Spark- 使用hiveContext时提交作业报错
在spark上操作hive时不需要搭建hive环境,只需要从现有的hive集群中hive的conf目录下拷贝 hive-site.xml 到spark的conf目录下即可提交程序运行 出现报错 Cau ...
- PHP的可变变量名
有时候可变的变量名会给编程带来很大的方便.也就是说变量名可以被动态的命名和使用.通常变量通过下面这样的语句来命名 : 1 2 3 <!--?php $a = 'hello'; ?--> 可 ...
- linux安装-----源码安装步骤--zlib软件安装
该zlib 可以对许多其他软件的编译代码起着优化 压缩作用. 解压压缩包: .tar.gz------------->tar zxvf 压缩包.tar.gz .tar.bz2---------- ...
- WPF 自定义依赖属性
原博客地址:http://www.cnblogs.com/DebugLZQ/archive/2012/11/30/2796021.html DependencyObject和Dependen ...
- 51nod 1270 dp
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1270 简单的线性dp,最近狂刷水题真的是...药丸 差值最大得话要么是峰 ...
- pxcook-高效易用的自动标注工具, 生成前端代码
1.pxcook.sketch(http://www.fancynode.com.cn/pxcook)