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. 解 ...
随机推荐
- es6 新增数据类型Symbol
es6在string number boolean null undefined object之外又新增了一种Symbol类型. Symbol意思是符号,有一个特性—每次创建一个Symbol值都是不一 ...
- Java实现Redis消息队列
这里我使用Redis的发布.订阅功能实现简单的消息队列,基本的命令有publish.subscribe等. 在Jedis中,有对应的java方法,但是只能发布字符串消息.为了传输对象,需要将对象进行序 ...
- 使用jquery获取父元素或父节点
使用jquery获取父元素或父节点,比较简单,jquery提供了丰富的方法来让我们使用jquery获取父元素或父节点 jquery获取父元素方法比较多,比如parent(),parents(),c ...
- Codeforces 342D Xenia and Dominoes 状压dp
码就完事了. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define ...
- 高性能之js
alloyteam团队的总结: 链接在:http://www.alloyteam.com/2012/10/high-performance-front-end-high-performance-jav ...
- Linux基础学习(一)__后台运行Python文件
Linux 后台运行Python脚本 1.安装Python:(python 3.5.4) 2.安装Python依赖包: 2.1 处理Python更新后yum无法正常使用的问题 (错误信息: -bash ...
- LoadRunner脚本参数化之设置条件与运行结果说明
性能测试中为什么需要进行参数化? 1.功能方面:首先要保证脚本的功能完善.可用性.(一般来说,参数化主要针对业务中的具备唯一性的数据.) 2.性能方面:一般来说,如果服务器存在缓存机制,在测试过程中, ...
- context-param和init-param的区别
http://www.cnblogs.com/hzj-/articles/1689836.html
- Unable to load configuration. - action - file:/F:/apache-tomcat-8.0.30/webapps/test1Struts2/WEB-INF/classes/struts.xml:11:71
Unable to load configuration. - action - file:/F:/apache-tomcat-8.0.30/webapps/test1Struts2/WEB-INF/ ...
- Java常用API——时间类
前言:Java.util.*工具包中,包含了集合框架,旧集合类,事件模型,日期和时间设施,国际化和其他使用程序类 (字符串.随机数生成器和位数组) 一.日期类Date 1.概述 Date是一个薄包装类 ...