刷题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, 力 ...
随机推荐
- 金币(0)<P2015_1>
金币 (coin.cpp/c/pas) [问题描述] 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收 ...
- LeetCode 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...
- Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群
一.集群介绍 根据功能划分为两大类:高可用和负载均衡 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 实现高可用的开源软件有:heartbeat. ...
- P1120/UVA307 小木棍(sticks) 题解
题目描述 pdf 题解 注意的问题是,各个原始木棒的长度都是一样的! 说一下本题的总思路即:DFS+超强力剪枝!(详见本人的 AC 程序) 首先,我们要从小到大枚举原始木棒的长度len,也就是枚举答案 ...
- 通过命令行提交更新代码到gitlab上
解决方法: 1.打开命令行的窗口,定位到项目所在的路径. 2.输入:git status,敲回车查看代码是否有更新,有更新的话会出现文件改变的文件名.(红色的) 3.输入:git commit -a ...
- Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
在广播中启动一个Dialog时出现如下错误信息:Caused by: android.view.WindowManager$BadTokenException: Unable to add windo ...
- lib文件和dll文件
一. 简介 1.1 C++两种库文件 lib包含了函数所在的dll文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的dll提供,称为动态链接库dynamic link library. ...
- Spark实验汇总(七个实验相结合)
日期:2020.01.20 博客期:128 星期一 一.环境搭建篇 1.安装虚拟机应用程序 VMware Workstation Pro [编写日期:2020-01-20] 去到 官网 下载 VMwa ...
- linux查漏补缺-linux命令行安装mysql
apt安装 sudo apt-get update sudo apt-get install mysql-server root@192:/sys/fs/cgroup# apt-get install ...
- python中主要存在的四种命名方式:
1.object -- 公用方法 2._object -- 办保护 (1)被看做是‘protect’,意思是只用类对象和自来对象自己能访问的变量 (2)在模块或类外不可以使用,不能用 ‘from m ...