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. 解 ...
随机推荐
- nginx做代理离线下载插件
一.背景 被安装的服务器不能上网,无法下载插件,一个插件都还好,但是遇到插件依赖很强的需要几十个插件的依赖,这样就很麻烦. 二.环境 192.168.182.155 安装nginx 能 ...
- 构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(66)-MVC WebApi 用户验证 (2)
前言: 构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(65)-MVC WebApi 用户验证 (1) 回顾上一节,我们利用webapi简单的登录并 ...
- ios线程和GCD和队列同步异步的关系
1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...
- iis url rewrite http->https non-www->www
<system.webServer> <rewrite> <rules> <rule name="Redirect abc.com to www&q ...
- 正则表达式在python中的简单使用
正则表达式独立与编程语言,基本上所有的编程语言都实现了正则表达式的相关操作.在Python中正则表达式的表现为re模块: import re 其操作有三个方法: my_string = "h ...
- gradle上传本地文件到远程maven库(nexus服务器)
自定义aar-upload.gradle文件 artifacts { archives file('./build/outputs/aar/Lib_ads-baidu-debug.aar') } up ...
- Stm32串口通信(USART)
Stm32串口通信(UART) 串口通信的分类 串口通信三种传递方式 串口通信的通信方式 串行通信的方式: 异步通信:它用一个起始位表示字符的开始,用停止位表示字符的结束.其每帧的格式如下: 在一帧格 ...
- window下的计划任务
0x00前言: 这几天看了看信息安全就业的面试题,其中有一条是计划任务如何设置,好几个月前稍微接触了,但是很久没用差不多都忘了>_<,这里就稍微学习下windows的计划任务 写着写着就偏 ...
- HTTP STATUS CODE: 521的解决办法
https://blog.csdn.net/wangdepei/article/details/84798601
- 3466 ACM Proud Merchants 变形的01背包
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:假设你有M元,已经Pi,Qi,Vi(i为角标,1<i<N),当M>Qi,时才 ...