[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 ...
随机推荐
- 模拟 + 暴搜 --- Help Me with the Game
Help Me with the Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3175 Accepted: ...
- 13. Scala函数式编程(高级部分)
13.1 偏函数(partial function) 13.1.1 需求 -> 思考 一个集合val list = List(1,2,3,4,"abc"),完成如下要求 1) ...
- 物联网通讯协议:MQTT,CoAP,NB-IOT,RFID,BLUETOOTH,NFC
一.按网络四层协议分类: NB-IoT,LORA,WIFI,蓝牙,zigbee,4G都是物理层的,这几个都需要芯片模组支持(硬件支持) 而MQTT,COAP,HTTP都是应用层协议,这些需要开发服务器 ...
- Elasticsearch 史上最全最常用工具清单
基础类工具 1.Head插件 1)功能概述: ES集群状态查看.索引数据查看.ES DSL实现(增.删.改.查操作) 比较实用的地方:json串的格式化 2)地址:http://mobz.github ...
- 浅谈maven setting.xml 设置的mirrorof标签作用。
https://blog.csdn.net/whbing1471/article/details/53983779 A 看这一段 背景:写好的java项目放置到linux服务器上进行编辑的时候,由于m ...
- Vue--基础1
目录 Vue MVVM模式 Hello World示例 挂载点 插值表达式与过滤器 Vue指令 文本指令 属性指令 事件指令 表单指令 条件指令 pre指令 循环指令 案例 Vue Vue是一个轻量级 ...
- DOM创建节点
1.DOM--document object model 常用的节点类型: 元素节点:(标签) 属性节点:(标签里的属性) 文本节点:(文本节点) 2,document有个属性叫nodetype,返回 ...
- rf中的条件判断与循环
条件判断 run keyword if condition name *args ... ELSE IF conditon name *args ...ELSE name *args conditio ...
- scala快速入门之文档注释
scala快速入门之文档注释 1.在项目栏的目录树中找到该源码,右击点击Show in Explorer, 即可找到该源码的本地路径,在路径中输入cmd 2.执行scaladoc -d 生成文档注释 ...
- ORA-12514: 监听程序当前无法识别连接描述符中请求的服务
/** 异常:ORA-12514: 监听程序当前无法识别连接描述符中请求的服务 * 背景:在很长一段时间都在连接远程开发库,曾偶尔有一次想要连接本地的库进行sql测试,发现连接失败,起初一直有无监听. ...