题目在这里: 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. 三元运算和lambda表达式

      19.三目运算,三元运算:     if else 的简写: name = 'alex' if 1 == 1 else 'SB'   ==> 等价于      if 1 == 1:     ...

  2. Python文件处理之文件打开方式(一)

    Python中打开一个文件是同过open函数来打开的,并返回一个文件对象,以下为open函数的参数: open(name[, mode[buf]]) name:文件路径 mode:打开方式 buf:缓 ...

  3. 一个正整数N,拆成任意个正整数之和,怎样使这些数的乘积最大

    网上看到了如标题所示的题目,就开始想如果用程序来算的话,那么它的算法是怎样的. 自己想了半天,第一感觉要用递归, 如先算出 当 n=1 max=1 当 n=2 max=1 当 n=3 max=2 .. ...

  4. SQL通过xml插入批量数据

    存储过程: CREATE PROCEDURE [dbo].[UP_PurchasexxxCard] @OrderInfo XMLASBEGIN SET NOCOUNT ON; DECLARE @Dat ...

  5. 深入理解JavaScript Hijacking原理

    最近在整理关于JavaScript代码安全方面的资料,在查关于JavaScript Hijacking的资料时,发现关于它的中文资料很少,故特意整理一下. 一.JavaScript Hijacking ...

  6. nginx对比haproxy 的反向代理

    -bash-4.1# ip add | grep inet inet 172.17.0.7/16 scope global eth0 inet6 fe80::42:acff:fe11:7/64 sco ...

  7. 【HDOJ】1513 Palindrome

    DP,MLE后改为滚动数组AC. #include <cstdio> #include <cstring> #include <cstdlib> #define M ...

  8. guestfish 修改 image file

    Example guestfish sessionSometimes, you must modify a virtual machine image to remove any traces of ...

  9. hadoop-2.6.0为分布式安装

    hadoop-2.6.0为分布式安装 伪分布模式集群规划(单节点)------------------------------------------------------------------- ...

  10. Myeclipse中java文件注释格式设置

    点击菜单windows->preferences,然后在左侧栏选择java ->Code Style -> CodeTemplates然后在右侧栏选择comments -> 依 ...