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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

题目

合并两个链表

思路

用dummy, 因为需要对头结点进行操作

代码

 /*
Time: O(min(m,n)) 因为一旦l1或l2有一方先遍历完,循环结束
Space: O(1) 因为没有额外多开空间
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// corner case
if (l1 == null) return l2;
if (l2 == null) return l1; ListNode dummy = new ListNode(-1);
ListNode p = dummy; while(l1 != null && l2 != null){
if (l1.val > l2.val){
p.next = l2;
l2 = l2.next;
}else{
p.next = l1;
l1 = l1.next;
}
p = p.next;
}
p.next = l1 != null ? l1 : l2;
return dummy.next;
}
}

[leetcode]21. Merge Two Sorted Lists合并两个链表的更多相关文章

  1. LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  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 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  4. [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 ...

  5. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  6. [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 ...

  7. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  8. 21. Merge Two Sorted Lists(合并2个有序链表)

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  9. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

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

随机推荐

  1. nginx upstream轮询配置

    nginx upstream nginx的upstream官方地址为:http://nginx.org/cn/docs/http/ngx_http_upstream_module.html 轮询分为多 ...

  2. PCL中Sample_consensus分割支持的几何模型

    随机采样一致分割法,从点云中分割出线.面等几何元素 #include <pcl/sample_consensus/method_types.h> #include <pcl/samp ...

  3. Browser Render Engine & Javascript Engine

    Browser Render Engine Programming Language Open Source Javascript Engine Comparation for CSS Compati ...

  4. Substring方法(C#,JS,Java,SQL)的区别

    C#: substring(第一参数,第二参数)//  第一参数:从第几位开始截,初始是从0位开始  第二参数:截取几位 substring(参数)  如果传入参数为一个长整, 且大于等于0,则以这个 ...

  5. windows server系统查看tomcat版本

    线上服务器系统是windows server版本,由于tomcat进行了apache httpd集群,所以对tomcat进行了统一编号: 在jprofiler中需要监听一个server实例,需要先搞清 ...

  6. # 20175227 2018-2019-2 《Java程序设计》第一周学习总结

    20175227 2018-2019-2 <Java程序设计>第一周学习总结 教材学习内容总结 1.安装VB,Ubuntu,Git,JDK,并自行配置. 2.写"Hello Wo ...

  7. android AES 加密解密

    import java.security.Provider; import java.security.SecureRandom; import javax.crypto.Cipher; import ...

  8. 回滚的意义---JDBC事务回滚探究

    JDBC手动事务提交回滚的常见写法一直是rollback写在commit的catch之后: try{ conn.setAutoCommit(false); ps.executeUpdate(); ps ...

  9. POJ1003 – Hangover (基础)

    Hangover   Description How far can you make a stack of cards overhang a table? If you have one card, ...

  10. RobotFramework - AppiumLibrary 之元素定位

    一.介绍 AppiumLibrary 是 Robot Framework 的App测试库. 它使用Appium 与Android 和 iOS应用程序进行通信,类似于Selenium WebDriver ...