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.


题目标签:Linked List

  题目给了我们两个lists,让我们有序的合并两个 lists。

  这题利用递归可以从list 的最后开始向前链接nodes,代码很简洁,清楚。

Java Solution:

Runtime beats 74.35%

完成日期: 06/09/2017

关键词:singly-linked list

关键点:利用递归从最后开始链接nodes

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public ListNode mergeTwoLists(ListNode l1, ListNode l2)
{
if(l1 == null)
return l2;
if(l2 == null)
return l1; if(l1.val < l2.val)
{
l1.next = mergeTwoLists(l1.next, l2);
return l1;
}
else
{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
} }

参考资料:

https://discuss.leetcode.com/topic/45002/java-1-ms-4-lines-codes-using-recursion

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 21. Merge Two Sorted Lists (合并两个有序链表)的更多相关文章

  1. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

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

  2. 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 ...

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

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

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

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

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

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

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

  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. 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 t ...

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

随机推荐

  1. 联想 Z5(L78011) 免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 10.5.254

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  2. HDU_1022_Train Problem I

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. cgroup代码浅析(1)

    前置:这里使用的linux版本是4.8,x86体系. cgroup_init_early(); 聊这个函数就需要先了解cgroup. cgroup概念 这个函数就是初始化cgroup所需要的参数的.c ...

  4. JAVA基础——Native关键字

    一:native声明 在Java中native是关键字.它一般在本地声明,异地用C和C++来实现.它的声明有几点要注意: 1)native与访问控制符前后的关系不受限制. 2)必须在返回类型之前. 3 ...

  5. 关于SPFA的双端队列优化

    7.11 Update 我做题的时候发现这样写会RE 因为在使用双端队列优化SPFA的时候 在将一个点加入队列的时候,如果队列已经空了 那么一旦出现dis[Q.front()]就会RE 可以这样修改 ...

  6. Ztree加载完成默认选中根节点右侧生成表格

    需求:页面加载完成之后,默认选中ztree的根节点,并执行其点击方法,右侧生成表格: 效果:如下图所示: 思路:在节点点击事件clickNode方法中根据节点的部门code查询这个部门下的所有员工,并 ...

  7. 集合:ListIterator

    why ? when ? how ? what ? Java 集合框架图 有了 Iterator 为什么还要有 ListIterator 呢? Iterator 遍历的时候如果你想修改集合中的元素怎么 ...

  8. Linux:RHEL7图形界面转文字

    1.7 Systemd初始化进程 Linux操作系统开机过程首先从BIOS开始→进入"Boot Loader"→加载系统内核→内核进行初始化→启动初始化进程.初始化进程作为系统第一 ...

  9. python基础学习 str,list,dict,set,range,enumerate

    一.字符串 s = 'python' s1 = 'python' + 'learn' #相加其实就是简单拼接 s2 = 'python' * 5 #相乘其实就是复制自己多少次,再拼接在一起 字符串切片 ...

  10. orcad中注意的事情

    1.地的标识不能放到已经分配了网络的线上. 在用orcad画原理图的时候,把电源放到网络的时候需要特别的注意,如果,将电源地直接放到线上,而这根线又已经被分配了网络标号,那这个地会随已经分配了的网络号 ...