leetcode [64] merge tow sorted lists
之前忘记记录这题了,现在补上。
合并两个有序的list,要求是:
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.
不知道我如下做是不是符合题目要求呢。
/**
* 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;
ListNode *tmp = new ListNode();
ListNode *head = tmp;
while(l1 && l2)
{
if (l1 -> val <= l2 -> val)
{
tmp -> next = l1;
tmp = l1;
l1 = l1 -> next;
}
else
{
tmp -> next = l2;
tmp = l2;
l2 = l2 -> next;
}
}
if (l1)
{
tmp -> next = l1;
}
else
{
tmp -> next = l2;
}
return head -> next;
}
};
leetcode [64] merge tow sorted lists的更多相关文章
- 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. 解 ...
- [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 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
- [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] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
随机推荐
- ash
查看当前用户使用的Shell,echo $SHELL BusyBox ('01-current) The BusyBox distribution is aiming for small implem ...
- MySQL SQL分析(SQL profile)
分析SQL优化运营开销SQL的重要手段.在MySQL数据库.可配置profiling参数启用SQL分析.此参数可以在全局和session水平集.级别则作用于整个MySQL实例,而session级别紧影 ...
- Python基本语法[二],python入门到精通[四] (转)
写在前面 python你不去认识它,可能没什么,一旦你认识了它,你就会爱上它 回到顶部 v正文开始:Python基本语法 1.定义常量: 之所以上篇博客介绍了定义变量没有一起介绍定义常量,是因为Pyt ...
- python派QQ邮件
#encoding=utf-8 __author__ = 'ds' #文件名冲突 from email.mime.text import MIMEText import smtplib if __na ...
- CentOS 7安装配置Apache HTTP Server
原文 CentOS 7安装配置Apache HTTP Server RPM安装httpd # yum -yinstall httpd //安装httpd会自动安装一下依赖包: apr apr-ut ...
- async和await关键字实现异步编程
async和await关键字实现异步编程 异步编程 概念 异步编程核心为异步操作,该操作一旦启动将在一段时间内完成.所谓异步,关键是实现了两点:(1)正在执行的此操作,不会阻塞原来的线程(2)一旦 ...
- Cannot update paths and switch to branch at the same time(转)
当使用git进行操作: git checkout -b local-name origin/remote-name 出现错误: fatal: git checkout: updating paths ...
- 第1章 单例模式(Single Pattern)
原文 第1章 单例模式(Single Pattern) 单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 一.常用模式: 1 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(15)-用户登录详细错误和权限数据库模型设计
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(15)-用户登录详细错误和权限数据库模型设计 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) ...
- linux_UBUNTU 12.04 上使用 SQUID 架设HTTP正向代理服务器
配置普通HTTP正向代理 安装 1 sudo apt-get install squid squid-common 配置 squid3 1 sudo vim /etc/squid3/squid ...