lettcode21. Merge Two Sorted Lists
lettcode21. 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.
从小到大排列的两个数组,合并成一个数组。递归的方法。注意:两个数组都为空的情况。
This solution is not a tail-recursive, the stack will overflow while the list is too long
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==NULL) return l2;
if(l2==NULL) return l1; if(l1->val>l2->val){
ListNode *tmp=l2;
tmp->next=mergeTwoLists(l1,l2->next);
return tmp;
}
else{
ListNode *tmp=l1;
tmp->next=mergeTwoLists(l1->next,l2);
return tmp;
}
}
};
2.新建了一个临时列表tmp,用时12ms,比上面多3ms(是什么原因呢?)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode head(-1);
ListNode *tmp=&head;
while(l1&&l2){
if(l1->val<l2->val){
tmp->next=l1;
l1=l1->next;
}else{
tmp->next=l2;
l2=l2->next;
}
tmp=tmp->next;
}
if(l1)
tmp->next=l1;
if(l2)
tmp->next=l2;
return head.next;
}
};
lettcode21. Merge Two Sorted Lists的更多相关文章
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] 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 ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- 71. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge Two Sorted Lists
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...
- Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
随机推荐
- Java基础知识➣泛型整理(四)
概述 泛型的本质是参数化类型,使用同一套代码来满足不同数据类型的业务需要,提高代码的执行效率,使代码简单明了. 泛型方法 该方法在调用时可以接收不同类型的参数.根据传递给泛型方法的参数类型,编译器适当 ...
- 前端本地存储localStorage
1.突破cookie 4K限制,一般浏览器支持5M 2.增 删 改 查 <!DOCTYPE html> <html lang="en"> <head& ...
- Flink--time-window 的高级用法
1.现实世界中的时间是不一致的,在 flink 中被划分为事件时间,提取时间,处理时间三种. 2.如果以 EventTime 为基准来定义时间窗口那将形成 EventTimeWindow,要求消息本身 ...
- phpmyadmin详细的图文使用教程
做网站用到服务器有很多站长应该都会用到数据库,那么phpmyadmin的使用也会是很多新手站长头大的问题,下面小编详细介绍一下phpmyadmin详细的图文使用教程. 方法/步骤 如何进入ph ...
- 启动 ServiceFabric Windows服务报1053
Remote Procedure Call (RPC) Locator和 Windows Firewall是否启动. 以管理员身份运行PowerShell,输入Unregister-Scheduled ...
- 实验3 敏捷开发与XP实践实验报告
一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:高君天 学号:20165319 指导教师:娄嘉鹏 实验日期:2018年4月27日 实验时间:13:45 - 3:25 实验序号:实验三 ...
- mybatis提示Invalid bound statement (not found)错误的可能原因
https://www.cnblogs.com/liaojie970/p/8034525.html
- 爬虫3 requests基础之 乱码编码问题
import requests res = requests.get('http://www.quanshuwang.com') res.encoding = 'gbk' print(res.text ...
- TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例—Jason niu
import tensorflow as tf # 22 scope (name_scope/variable_scope) from __future__ import print_function ...
- elementUI Tree 树形控件--官方文档
一.基础用法基础的树形结构展示,props相当于一个对实体类对像 <template> <el-tree :data="data" :props="de ...