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 ...
随机推荐
- windchill系统——开发_角色管理——增加角色
步骤如下 ResourceBuild wt.project.RoleRB ant -f codebase/MakeJar.xml 这样就完成了,接下来看看结果
- MYSQL 多实例运行
1.创建数据文件 mkdir /var/lib/mysql_3307 mysql_install_db --datadir=/var/lib/mysql_3307 --user=mysql 2.给数据 ...
- JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体
1. struts 工作流程图 超链接 2. 入门案例 struts入门案例: 1.写一个注册页面,把请求交给 struts处理 <form action="${pageContext ...
- LeetCode第[34]题(Java):Search for a Range
题目:搜索目标范围 难度:Medium 题目内容: Given an array of integers nums sorted in ascending order, find the starti ...
- Educational Codeforces Round 38
http://codeforces.com/contest/938 A:sb题 //#pragma comment(linker, "/stack:200000000") //#p ...
- Win7 SP1 下安装 VS2015 Update 3
首先Win7必须是SP1,然后要求安装IE11,安装IE11前需要安装6个先决补丁,成功安装IE11后建议安装3个可选补丁.然后导入3个根证书(COMODO RSA Certification Aut ...
- uva 10305 拓扑排序裸题
https://vjudge.net/problem/UVA-10305 目前没学dfs做法,用的队列做法,每次找到一个入度为零的点出队后更新其他点,再加入入度为零的点直到查找完毕,这个题目显然一定有 ...
- 【IDEA】笔记
引言 IDEA是JAVA开发的一个神器,熟悉它能极大提高我们的开发效率.正所谓工欲善其事,必先利其器. 快捷键 快捷键 介绍 Ctrl + F 在当前文件进行文本查找 (必备) Ctrl + R 在当 ...
- LeetCode OJ:Roman to Integer(转换罗马字符到整数)
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [置顶]
Xposed也要热更新
好久没写博客了.这次玩一点不一样的. 吐槽&起因 相信熟悉Xposed的小伙伴们都知道,每次写完Xposed都要重启啊!有木有!反射错了,写错了名字,改一个log,都要重启啊有木有!重启浪费时 ...