[Algorithm] 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 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的更多相关文章
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 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 ...
- 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 ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- 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 ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- 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 ...
- [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 ...
- [leetcode] 21. Merge Two Sorted Lists (Easy)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
随机推荐
- Effective.Java第23-33条(泛型相关)
23. 类结构层次优于标签类 有时你会碰到一个类,它的实例有一个或多个风格,并且包含一个tag属性表示实例的风格.例如,如下面的类表示一个圆或者矩形: public class Figure { / ...
- Go语言【项目】 websocket消息服务
websocket消息服务 目的:搭建websocket服务,用浏览器与服务进行消息交互(写的第一个Go程序) 代码目录结构: 前端html页面: <!DOCTYPE html> < ...
- FPGA控制RGMII接口PHY芯片基础
一.前言 网络通信中的PHY芯片接口种类有很多,之前接触过GMII接口的PHY芯片RTL8211EG.但GMII接口数量较多,本文使用RGMII接口的88E1512搭建网络通信系统.这类接口总线位宽小 ...
- vip视频会员共享电影免费看
vip会员在线看是一款可以观看持有会员特权的视频才能看的网站程序,小而强大,目前支持所有视频网站解析! 解析需要时间耐心等待20秒,如果加载失败,请切换接口耐心等待20秒! 找要要看的vip电影地址, ...
- 『正睿OI 2019SC Day8-Day17』
于是就迎来\(10\)天的自闭考试了,每天写点小总结吧. Day8 第一天就很自闭啊,考题分别是数学题+建模题+图论. 前两道题都没有什么算法,但是难度还是有的,于是就做不太出来,特别是第一题.第二题 ...
- Unable to connect to HBase using Phoenix JDBC Driver
Feb 01, 2017; 5:21pm Unable to connect to HBase using Phoenix JDBC Driver 9 posts Hi All, I am try ...
- MVC伪静态路由简单搭配
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute ...
- WCF学习笔记(一)---我的第一个WCF程序
一.创建WCF程序 1.创建一个控制台程序(WCFBlog) 2.添加wcf项目 3.将默认的IService1和Service1改成自己的名字 4.在ICalculateServic ...
- drf序列化与反序列化
序列化器-Serializer 定义序列化器 Django REST framework中的Serializer使用类来定义,须继承自rest_framework.serializers.Serial ...
- Java 之 线程安全(线程同步)
一.线程安全 当有多个线程同时运行,而这些线程可能会同时运行这段代码.程序每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,这就是线程安全的. 下面通过一个案例来演示线程的 ...