【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列
(一)题目
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.
(二)解题
这题是剑指offer上的老题了,剑指上面用的是递归,我写了个非递归的版本。
/**
* 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) {
ListNode* head = NULL;
if(l1==NULL && l2==NULL) return head;
ListNode* p = NULL;
ListNode* p1 = l1;
ListNode* p2 = l2;
while(p1 != NULL || p2!=NULL)
{
if(p1 != NULL && p2 != NULL)
{
if(p1->val < p2->val)
{
if(head == NULL) //记录头节点head
{
head = p1;
p = head;
}
else {p->next = p1;p=p->next;}
p1=p1->next;
}
else
{
if(head == NULL)
{
head = p2;
p = head;
}
else {p->next = p2;p=p->next;}
p2=p2->next;
}
}
if(p1 == NULL && p2 != NULL)
{
if(head == NULL)
{
head = p2;
p = head;
}
else {p->next = p2;p=p->next;}
p2=p2->next;
}
if(p1 != NULL && p2 == NULL)
{
if(head == NULL)
{
head = p1;
p = head;
}
else {p->next = p1;p=p->next;}
p1=p1->next;
}
}
return head;
}
};
递归版本:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
ListNode* head;
if(l1->val < l2->val)
{
head = l1;
head->next = mergeTwoLists(l1->next , l2);
}
else
{
head = l2;
head->next = mergeTwoLists(l1 , l2->next);
}
return head;
}
};
【一天一道LeetCode】#21. Merge Two Sorted Lists的更多相关文章
- [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 混合插入有序链表
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)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
- leetcode 21.Merge Two Sorted Lists ,java
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 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
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- Java [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 spli ...
- [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(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
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- Android文件大头10G
这个玩意直接10G....记录下. C:\Users\xn\AppData\Local\Android\sdk\system-images\android-23
- Openstack: change endpoint IP addresses after installation
Prerequisites You have a single node DevStack installation using mysql for the database that was wor ...
- Microsoft Dynamics 365 Developer Toolkit下载地址
下载,支持Visual Studio 2012, 2013, 2015
- 【SSH系列】初识spring+入门demo
学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍 ...
- Hadoop的RPC通信原理
RPC调用: RPC(remote procedure call)远程过程调用: 不同java进程间的对象方法的调用. 一方称作服务端(server),一方称为客户端(client): server端 ...
- 使用Xpath定位元素(和元素定位相关的Xpath语法)
本文主要讲述Xpath语法中,和元素定位相关的语法 第一种方法:通过绝对路径做定位(相信大家不会使用这种方式) By.xpath("html/body/div/form/input" ...
- 多线程(三) 实现线程范围内模块之间共享数据及线程间数据独立(ThreadLocal)
ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.JDK 1.2的版本中就提供java.lang.ThreadLocal,使用这个工具类可以很简洁地编写出优美的多线程程序,Threa ...
- 详解EBS接口开发之库存事务处理-物料批次导入
库存事务处理-物料批次导入 --系统批次表 SELECT * FROM MTL_LOT_NUMBERS T; --API创建批次 inv_lot_api_pub.create_inv_lot(x_re ...
- android PM2.5监控demo开发
最近看到了这个网站是aqicn.org,是一个监控北京空气状态的网站,截图如下 好了,接下来我们利用这个网站返回的json数据来写一个监控北京空气状况尤其是PM2.5的demo. 1.布局文件如下: ...
- Retrofit 2.0 超能实践(三),轻松实现文件/多图片上传/Json字符串
文:http://blog.csdn.net/sk719887916/article/details/51755427 Tamic 简书&csdn同步 通过前两篇姿势的入门 Retrofit ...