刷题21. Merge Two Sorted Lists
一、题目说明
这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表。难度是Easy!
二、我的解答
既然是简单的题目,应该一次搞定。确实1次就搞定了,但是性能太差:
Runtime: 20 ms, faster than 8.74% of C++ online submissions for Merge Two Sorted Lists.
Memory Usage: 9.4 MB, less than 5.74% of C++ online submissions for Merge Two Sorted Lists.
代码如下:
class Solution{
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2){
if(l1 ==NULL && l2==NULL) return NULL;
if(l1 !=NULL && l2==NULL) return l1;
if(l1 ==NULL && l2!=NULL) return l2;
ListNode dummy(-1);
ListNode* p = &dummy;
while(l1 !=NULL && l2 !=NULL){
if(l1->val <= l2->val){
p->next = l1;
p = p->next;
l1 = l1->next;
}else{
p->next = l2;
p = p->next;
l2 = l2->next;
}
}
if(l1 !=NULL){
p->next = l1;
}
if(l2 !=NULL){
p->next = l2;
}
return dummy.next;
}
};
三、优化措施
优化后,8s,代码如下:
#include<iostream>
using namespace std;
struct ListNode{
int val;
ListNode*next;
ListNode(int x):val(x),next(NULL){
}
};
/**
* 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(NULL == l1) return l2;
if(NULL == l2) return l1;
ListNode dummy(-1);
ListNode* p = &dummy;
while(l1 && l2 ){
if(l1->val <= l2->val){
p->next = l1;
l1 = l1->next;
}else{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
p->next = l1 ? l1 : l2;
return dummy.next;
}
};
int main(){
Solution s;
ListNode* l1,*l2;
ListNode* tmp;
//init l1
tmp = new ListNode(4);
l1 = tmp;
tmp = new ListNode(2);
tmp->next = l1;
l1 = tmp;
tmp = new ListNode(1);
tmp->next = l1;
l1 = tmp;
//init l2
tmp = new ListNode(4);
l2 = tmp;
tmp = new ListNode(3);
tmp->next = l2;
l2 = tmp;
tmp = new ListNode(1);
tmp->next = l2;
l2 = tmp;
ListNode* l3 = s.mergeTwoLists(l1,l2);
while(l3!=NULL){
cout<<l3->val<<" ";
l3 = l3->next;
}
return 0;
}
刷题21. Merge Two Sorted Lists的更多相关文章
- 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 ...
- 刷题23. Merge k Sorted Lists
一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...
- [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 ...
- 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 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
随机推荐
- win8.1 virtualbox 安装centos7注意事项
win8.1是64位的,一开始在virtualbox中选择版本时,怎么也选不到64位的,这时要改BIOS设置,把CPU虚拟化改为允许. virtualbox是32位的,没必要非得是64位(64位的也不 ...
- Mac旧机「焕」新机过程记录
一.首先我做了非硬件上的优化处理,在升级到10.14之前还是挺管用的.但是为了使用最新的iOS SDK,升级到10.14以后,已经不管用了. 1.设置->通用 将动画相关的选项去掉. 2.设置- ...
- nginx 与上游服务器建立连接的相关设置
向上游服务建立联系 Syntax: proxy_connect_timeout time; #设置TCP三次握手超时时间,默认60秒:默认超时后报502错误 Default: proxy_connec ...
- lua叠代器
注意:叠待值遇到nil就退出 叠代器,是符合for遍历框架,需要满足条件 1-叠代函数,常量,控制变量 2-叠代函数可以接受二个参数,当然也可以忽略处理(利用闭包封装参数作为控制变量和状态变量) 无状 ...
- Spring学习(二)
IoC 1.Inverse of Control ,控制反转(控制权的翻转) 2.控制:对对象的创建.对对象的属性赋值等一系列操作本来应该是我们做的事情 Java Application : Date ...
- 【转】python中的闭包详细解析
一.什么是闭包? 如果一个内嵌函数访问外部嵌套函数作用域的变量,并返回这个函数,则这个函数就是闭包 闭包必须满足三个条件: 1. 必须有一个内嵌函数 2. 内嵌函数必须引用外部嵌套函数中的变量 ...
- Python基础-1 基础语法
基础语法 标识符 所谓的标识符就是对变量.常量.函数.类等对象起的名字. 首先必须说明的是,Python语言在任何场景都严格区分大小写!也就是说A和a代表的意义完全不同 python对于表示标识符的命 ...
- LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)
题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...
- Python学习第十二课——json&pickle&XML模块&OS模块
json模块 import json dic={'name':'hanhan'} i=8 s='hello' l=[11,22] data=json.dumps(dic) #json.dumps() ...
- 02-10Android学习进度报告十
今天我学习了有关ListView的基础知识,主要是学习了其中界面展示的基本方法. 首先看一个简单的列表实现代码: public class Animal { private String aName; ...