Leetcode 21. Merge Two Sorted Lists(easy)
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;
} if (l1 -> val <= l2 -> val){
l1 -> next = mergeTwoLists(l1 -> next, l2);
return l1;
}
else{
l2 -> next = mergeTwoLists(l1, l2 -> next);
return l2;
} }
};
非递归实现:
/**
* 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 head(); // 新建一个结点,不能是指向空的指针,否则它的指向不会改变
ListNode * cur = &head;
while (l1 && l2){
if (l1 -> val <= l2 -> val){
cur -> next = l1;
l1 = l1 -> next;
}else{
cur -> next = l2;
l2 = l2 -> next;
}
cur = cur -> next;
}
if (l1){
cur -> next = l1;
}
if (l2){
cur -> next = l2;
}
return head.next; // 注意head此时为一个实体对象,不是一个指针,所以要用.来指示next.为什是next??
}
};
Leetcode 21. Merge Two Sorted Lists(easy)的更多相关文章
- 【leetcode】Merge Two Sorted Lists(easy)
		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(Easy)
		1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ... 
- 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 [Difficulty: Easy]
		题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ... 
- 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 (有序两个链表整合)
		题目链接 https://leetcode.com/problems/merge-two-sorted-lists/?tab=Description Problem: 已知两个有序链表(链表中的数 ... 
- LeetCode 21. Merge Two Sorted Lists(c++)
		要定义两个链表 判断时依次对应每一个链表的值进行判断即可. /** * Definition for singly-linked list. * struct ListNode { * int val ... 
- LeetCode 21. Merge Two Sorted Lists(合并两个有序链表)
		题意:合并两个有序链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ... 
- 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 ... 
随机推荐
- C#多线程编程的同步也线程安全
			前一篇文章记录了简单的多线程编程的几种方式,但是在实际的项目中,也需要等待多线程执行完成之后再执行的方法,这个就叫做多线程的同步,或者,由于多个线程对同一对象的同时操作造成数据错乱,需要线程安全.这篇 ... 
- MySQL 笔记整理(3) --事务隔离,为什么你改了我还看不见?
			笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> 3) --事务隔离,为什么你改了我还看不见? 简单来说,事务就是要保证一组数据操作,要么全部成功,要么全部失败.在MySQL中,事务 ... 
- sqlserver数据库发送邮箱
			Exec [msdb].dbo.sp_send_dbmail @profile_name='SQLMailConfig', @recipients = @email, //需要发送的邮箱 @su ... 
- RabbitMQ如何工作和RabbitMQ核心概念
			RabbitMQ是一个开源的消息代理软件.它接受来自生产者的消息并将其传递给消费者.它就像一个中间人,可以用来减少Web应用程序服务器的负载和交付时间. RabbitMQ如何工作 让我们简要介绍一下R ... 
- offic|集成|协同OA|移动办公|
			随着互联网时代的日新月异,移动通讯技术的飞速发展,移动网络技术的更新换代,手机.平板电脑等移动设备越来越智能化.越来越多样化,人们对移动办公的需求也在日益增长.在此背景下北京博信施科技有限公司自主研发 ... 
- iOS---------- Safe Area Layout Guide before iOS 9.0
			如果你们的项目不做iOS9以下支持就打开main.storyboard 去除Use safe Area Layout 如果不考虑iOS9以下支持就按照下面的步骤 选中控制器,右边面板的Build ... 
- adb server is out of date. killing完美解决
			原本是想跑monkey测试的,可使用adb命令时提示:adb server is out of date. killing... 出现这个问题的原因是:adb使用的端口5037被占用了.下面我们说下如 ... 
- 电脑一键U盘启动快捷键
			下面是我特意列出的品牌电脑.笔记本电脑.组装电脑一键U盘启动快捷键对应列表,仅供大家查阅参考! [品牌-笔记本电脑] 笔记本品牌 启动按键 联想笔记本 F12 宏基笔记本 F12 华硕笔记本 ... 
- Linux(DeepInOS) 下 mysql 的安装与基本配置
			索引: 目录索引 参看代码 GitHub: DeepIn(GNU/Linux) MySQL 一.安装 sudo apt-get install mysql-server 期间需要输入两次密码,root ... 
- PJSUA2开发文档--第十二章 PJSUA2 API 参考手册
			12 PJSUA2 API 参考手册 12.1 endpoint.hpp PJSUA2基本代理操作. namespace pj PJSUA2 API在pj命名空间内. 12.1.1 class En ... 
