题目:

  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. nginx反向代理服务器端口问题

      nginx可以很方便的配置成反向代理服务器 server { listen 80; server_name bothlog.com; location / { proxy_set_header H ...

  2. Python caffe.TEST Example(Demo)

    下面提供了caffe python的六个测试demo,大家可以根据自己的需求进行修改. Example 1 From project FaceDetection_CNN-master, under d ...

  3. 自己写的JS排序算法

    这学期刚刚学完数据结构,之前就自己写了一点东西,现在整理一下. <!DOCTYPE html> <html> <head> <meta charset=&qu ...

  4. ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018

    ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syr ...

  5. GO学习笔记:函数作为值、类型

    在Go中函数也是一种变量,我们可以通过type来定义它,它的类型就是所有拥有相同的参数,相同的返回值的一种类型: type typeName func(input1 inputType1 , inpu ...

  6. WPF中的平移缩放和矩阵变换(TranslateTransform、ScaleTransform、MatrixTransform)

    在WPF中的平移缩放都是通过RenderTransform这个类来实现这些效果的,在这个类中,除了平移和缩放还有旋转.扭曲变换.矩阵变换.这些都差不多的,都是坐标的变换. 这里我就先简单弄个平移和缩放 ...

  7. lightoj1213推公式

    很容易推出来的公式ans=n^(k-1)*k*sum 然后快速幂就好了 #include<map> #include<set> #include<cmath> #i ...

  8. 全局ajax事件

    必须当页面上存在任何ajax请求的时候都将触发这些特定的全局ajax处理函数. 如果在jQuery.ajaxSetup()中的global属性设置成true,那么这些全局函数将会在每一个ajax上面都 ...

  9. 使用Jenkins自动编译我的 java 项目 git maven jenkins

    之前的项目已经将jenkins部署好,现在添加maven项目 准备工作 安装插件 Git plugin Publish Over SSH 全局设置  key: 是 linux服务器的私钥 Global ...

  10. LeetCode OJ:Happy Number(欢乐数)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...