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 ...
随机推荐
- LeetCode——Number of Boomerangs
LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...
- Action<T>和Func<T>
Action<T>和Func<T>都是泛型委托. Action<T>表示委托可以引用一个viod返回类型的方法,至于方法是带几个参数,什么类型的参数,由后面的泛型决 ...
- Oracle数据库常用监控语句
--在某个用户下找所有的索引 select user_indexes.table_name, user_indexes.index_name,uniqueness, column_name from ...
- 关于EventBus3.0使用,你看这篇就够了
作为一枚Android开发者,关于EventBus相信应该都听说过.要是用过就请忽略本文,本文讲得比较基础. 要是没用过,建议你花两分钟看看. 目前EventBus最新版本是3.0,本demo基于3. ...
- vector push_back数量大的时候变慢
才用15000个数据 push_back耗时就好几秒, 解决方法是 先resize 15000, 然后再 for (int i = 0; i < 15000; i++) { Data data ...
- tensorflow conv2d的padding解释以及参数解释
1.padding的方式: 说明: 1.摘录自http://stackoverflow.com/questions/37674306/what-is-the-difference-between-sa ...
- 通过使用Netty实现RPC
目标:通过使用Netty框架实现RPC(远程过程调用协议),技术储备为以后实现分布式服务框架做技术储备.在这里实现自定义协议主要实现远程方法调用. 技术分析: 1.通过Java的反射技术我们可以获取对 ...
- JQuery 全选 取消
$('#chkAllProp').click(function () { $("input[id*='chkSelect']").prop("checked", ...
- 分享知识-快乐自己:Ajax 跨域请求处理
<%-- Created by IntelliJ IDEA. User: asus Date: 2019/1/24 Time: 15:57 To change this template use ...
- iOS-iOS8模拟器设置中文键盘
1.解决不弹出虚拟键盘: 模拟器菜单->Hardware ->Keyboard, 点击Toggle Software Keyboard,虚拟键盘弹出 2.解决虚拟键盘没有中文输入法的问题: ...