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. 记一次php脚本memory exhausted

    表象报错如下: Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16651985 bytes) 出 ...

  2. [转]c++访问python3-实例化类的方法

    转自: http://blog.csdn.net/love_clc/article/details/76653100 此文是学习笔记,供日后翻阅.下面列出C++访问python所需的函数,按调用的先后 ...

  3. MySQL数据实时增量同步到Kafka - Flume

    转载自:https://www.cnblogs.com/yucy/p/7845105.html MySQL数据实时增量同步到Kafka - Flume   写在前面的话 需求,将MySQL里的数据实时 ...

  4. 理解java回调机制

    摘自:ImportNew 以前不理解什么叫回调,天天听人家说加一个回调方法啥的,心里想我草,什么叫回调方法啊?然后自己就在网上找啊找啊找,找了很多也不是很明白,现在知道了,所谓回调:就是A类中调用B类 ...

  5. Python 输出

    普通的输出 生活中的“输出”: 软件中的“输出”: python中变量的输出: print('hello world') 格式化输出 占位符% print('Hello,%s' % 'Python') ...

  6. JavaScript图形库

    做前端少不了画图,html5 的 canva 很强大,可是如果所有需求都自己画图,恐怕会被 leader 炒掉.记录一下我用过的几个 2D 和 3D 的js图形库,各种功能.我就不分 2D 和 3D ...

  7. EasyMock 模拟对象测试

    一.EasyMock 使用动态代理实现模拟对象创建,一般可以满足以下测试需求 1.要测试的模块依赖于其它自己控制不了的模块,如第三方服务,其它组员在开发的服务等,它们都没办法配合你来测试: 2.涉及到 ...

  8. 读完这个我懂了JNDI

    转载自:http://私塾在线/forum/blogPost/list/1186.html NDI 是什么 JNDI是 Java 命名与目录接口(Java Naming and Directory I ...

  9. windows异常事件对应的ID

    转载地址: Windows 2008 R2查看异常关机或开机事件ID https://blog.csdn.net/hejun1218/article/details/81059327

  10. 性能测试day07_性能瓶颈和分析

    其实如果之前都做的很到位的话,那么再加上APM工具(dynaTrace等),监控到非常细节,那么我们跑一个业务,我们就能完全清楚的知道每个请求的时间,也能知道请求所产生sql的时间,这样你自然而然都知 ...