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

Solution 1:

var mergeTwoLists = function(l1, l2) {
if (!l1 && !l2) {
return null;
} if (!l1 && l2) {
return l2;
} if (!l2 && l1) {
return l1;
} const smallerNode = l1.val <= l2.val ? l1 : l2;
if (smallerNode.val === l1.val) {
smallerNode.next = mergeTwoLists(l1.next, l2);
} else {
smallerNode.next = mergeTwoLists(l1, l2.next);
} return smallerNode;
}

Solution 2: Better

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) { let curr = temp = new ListNode(0) while (l1 && l2) {
if (l1.val < l2.val) {
curr.next = l1;
l1 = l1.next;
} else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
} if (l1) {
curr.next = l1;
} if (l2) {
curr.next = l2;
} return temp.next;
};

[Algorithm] 21. Merge Two Sorted Lists的更多相关文章

  1. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

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

  3. 21. Merge Two Sorted Lists【easy】

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

  4. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  5. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  6. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  7. C# 写 LeetCode easy #21 Merge Two Sorted Lists

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

  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. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

随机推荐

  1. 建造(Builder)模式

    建造模式可以将一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 摘自EffectiveJava:当构造方法参数过多时使用建造者模式. 产品的内部表象 ...

  2. setInterval的使用

    可以通过设置标识,判断方法是否执行完 var interval = setInterval(function () { if( flag > 0 ){ clearInterval(interva ...

  3. Dubbo快速入门 二

    2.dubbo核心概念 2.1).简介 Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能.轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调 ...

  4. java中各种常见的异常

    一.各种常见的异常 在上一节中程序如果你注意留意,程序抛出的异常是:java.lang.ArithmeticException.这个异常是在lang包中已经定义的.在lang包中还定义了一些我们非常常 ...

  5. 整理:WPF中应用附加事件制作可以绑定命令的其他事件

    原文:整理:WPF中应用附加事件制作可以绑定命令的其他事件 目的:应用附加事件的方式定义可以绑定的事件,如MouseLeftButton.MouseDouble等等 一.定义属于Control的附加事 ...

  6. 4、VUE生命周期

    下面是分步骤解释vue生命周期 1.开始:new Vue() 创建vue对象过程还是比较繁琐的,所以创建vue对象是异步执行的. 回调函数:beforeCreate 2.Observe Data 监控 ...

  7. CentOS8 安装MySQL8.0

    2019/11/25, CentOS 8,MySQL 8.0 摘要: CentOS 8 安装MySQL 8.0 并配置远程登录 安装MySQL8.0 使用最新的包管理器安装MySQL sudo dnf ...

  8. C#读写设置修改调整UVC摄像头画面-光圈

    有时,我们需要在C#代码中对摄像头的光圈进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...

  9. OpenResty下载安装教程

    原文链接:http://www.studyshare.cn/software/details/1174/0 一.OpenResty简介 OpenResty是一个全功能的 Web 应用服务器.它打包了标 ...

  10. 腾讯java面试经验 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.腾讯等公司offer,岗位是Java后端开发,因为发展原因最终选择去了腾讯,入职一年时间了,也成为了面试官,之 ...