题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/

【标签】Linked List

【题目分析】这个题目就是merge sort在 Linked List中的变形。不多说,直接上代码了

   public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(-1);
ListNode node = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
node.next = l1;
l1 = l1.next;
} else {
node.next = l2;
l2 = l2.next;
}
node = node.next;
}
// append the remaining list
node.next = (l1 != null) ? l1 : l2;
return dummyHead.next;
17  }

[Leetcode][021] Merge Two Sorted Lists (Java)的更多相关文章

  1. 【JAVA、C++】LeetCode 021 Merge Two Sorted Lists

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

  2. Leetcode 021 Merge Two Sorted Lists

    摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

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

  4. Java for LeetCode 023 Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

  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 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  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 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  9. [Leetcode Week4]Merge Two Sorted Lists

    Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...

随机推荐

  1. Hive笔记--sql语法详解及JavaAPI

    Hive SQL 语法详解:http://blog.csdn.net/hguisu/article/details/7256833Hive SQL 学习笔记(常用):http://blog.sina. ...

  2. Windows10 删除已经保存的WIFI热点

    自己的笔记本很多时候都是连接WIFI上网,导致保存的WIFI越来越多,有些都过期不能用了,但还是在列表中存在着,致使列表很长很难看,如下: 删除无用热点的方法如下: win+r运行cmd,进入命令行界 ...

  3. PHP中的常量

    常量与变量不一样,一旦被定义就可以全局访问. 例如: <?php define('CONST_NAME','shiyingyan'); define('RESULT_OK',1); define ...

  4. android弹出式菜单、弹出式对话框、弹出式窗口

    http://www.open-open.com/lib/view/open1389767042601.html http://www.open-open.com/lib/view/open13321 ...

  5. MPlayer播放器安装

    http://www.mplayerhq.hu/MPlayer/releases/MPlayer-1.1.1.tar.xz [tim@L MPlayer-1.1.1]$ mplayer         ...

  6. HDOJ 1395 2^x mod n = 1

    Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. Input ...

  7. HDOJ 1335 Basically Speaking(进制转换)

    Problem Description The Really Neato Calculator Company, Inc. has recently hired your team to help d ...

  8. 求一个数组中第K小的数

    面试南大夏令营的同学说被问到了这个问题,我的第一反应是建小顶堆,但是据他说用的是快排的方法说是O(n)的时间复杂度, 但是后来经过我的考证,这个算法在最坏的情况下是O(n^2)的,但是使用堆在一般情况 ...

  9. HDU2196-Computer

    原题连接: http://acm.hdu.edu.cn/showproblem.php?pid=2196 思路: 好了,无敌了,经过昨晚4个钟头+今上午1个小时的奋战,这题终于被我AC了 收获的确是不 ...

  10. 简易封装一个带有占位文字的TextView

    在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框: