一、题目说明

这个题目是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的更多相关文章

  1. 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 ...

  2. 刷题23. Merge k Sorted Lists

    一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...

  3. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  4. 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 ...

  5. 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 ...

  6. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  7. 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 ...

  8. [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 ...

  9. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

随机推荐

  1. maven项目引用外部jar包的方法

    问题描述: 有一个java maven web项目,需要引入一个第三方包gdal.jar,但是这个包是自己打包的,在maven中央库里面找不到该包,因此我采用传统的方式,将这个包拷贝到:项目名称\sr ...

  2. Jquery+ajax模板

    $.ajax({     url:'',     type:'POST', //GET     async:true,    //或false,是否异步     data:{         name ...

  3. scrapy-redis分布式

    scrapy是python界出名的一个爬虫框架,提取结构性数据而编写的应用框架,可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 虽然scrapy 能做的事情很多,但是要做到大规模的分 ...

  4. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  5. 七 Struts2访问Servlet的API方式二:原生方式

    Struts2访问Servlet的API方式二:原生方式 和解耦合的方式不同,原生方式既可以拿到域对象,也可以调用域对象中的方法 前端jsp: <%@ page language="j ...

  6. windows下代码规范检测工具sonarqube安装与使用,含与maven的结合

    一.首先下载sonarqube   地址 : https://www.sonarqube.org/downloads/   (最新版本支持java11+,博主下载支持java8的版本7.7), 下载S ...

  7. Node.js 阻塞 回调函数

    回调例程 N所有API都支持回调函数,可以处理大量并发请求.回调函数一般作为最后一个参数出现: function foo1(name, age, callback){ } function foo2( ...

  8. I Hate it-HDU1754 点修改+区间最大值

    题意: 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感.不管你喜不喜欢, 现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然, ...

  9. Vue 项目开发

    目录 Vue 项目开发 项目目录结构解析 入口文件 main.js (项目入口) 根组件 app.vue index.html 文件入口 router 路由 components 子组件 项目初始化 ...

  10. springboot#配置https

    1.准备证书 2.1 springboot 1.x配置 2.2 springboot 2.x配置 1.准备证书: keytool -genkeypair -alias tomcat -keyalg R ...