刷题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, 力 ...
随机推荐
- 笔记-capped collection
笔记-capped collection 1. collection 1.1. 简介 集合分为固定与非固定collection,capped collection 1.1.1. c ...
- JDBC笔记一
连接池原理 数据库连接池:1.提前创建好多个连接对象,放到缓存中(集合),客户端用时直接从缓存中获取连接 ,用完连接后一定要还回来. 目的:提高数据库访问效率. 模拟代码: package com. ...
- crm系统和e_store商场的比较总结
e_store用了:Java.Servlet.JSP.Oracle.JQuery.Mybatis,tomcat技术 crm用了 :Java.JSP.Oracle.JQuery,Mybatis,spri ...
- 十八 OGNL特殊符号的作用,#,%,$
主要有哪些字符? #:获取Context的数据,构建map %: 强制解析OGNL,强制不解析OGNL $ : 在配置文件中(xml,属性文件(国际化))使用OGNL #的用法: <body&g ...
- TensorFlow样例一
假设原函数为 f(x) = 5x^2 + 3,为了估计出这个函数,定义参数未知的函数g(x, w) = w0 x^2 + w1 x + w2,现要找出适合的w使g(x, w) ≍ f(x).将这个问题 ...
- day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)
1. 装饰器 / 类中的方法 / 类的方法变属性 # ### 装饰器 """ 定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数代码的 ...
- android下创建文件夹和修改其权限的方法
原文:http://www.cnblogs.com/wanqieddy/archive/2011/12/28/2304906.html 由于工作的需要,今天研究了在android下创建文件夹和修改其权 ...
- crontab Yii commands 使用方法
基本知识介绍 #crontab -u <-l, -r, -e> -u指定一个用户-l列出某个用户的任务计划-r删除某个用户的任务-e编辑某个用户的任务 cron文件语法与写法 Minute ...
- 10.MongoDB
1.安装(1.1):去官方下载最新的包,http://www.mongodb.org/downloads(1.2):然后tar zvxf 解压(1.3):拷贝到相应的文件夹即可2.在Shell里面启动 ...
- c# 事件3
1.什么是事件,使对象或者类具有通知功能的成员.//为了解决字段在外部被滥用,推出了事件 事件的功能能=通知+可选的事件参数(具体的详细信息,包括谁发送了消息,发送的什么消息) 使用:用于对象或者类件 ...