【一天一道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 ...
随机推荐
- mysql和postgresql转义字符探究
总结 mysql依靠反斜杠\转义, postgresql 依靠单引号转义 mysql 客户端 mysql> create table usr (name varchar(), age integ ...
- Bootstrap3 栅格系统-实例:多余的列(column)将另起一行排列
如果在一个 .row 内包含的列(column)大于12个,包含多余列(column)的元素将作为一个整体单元被另起一行排列. <div class="row"> &l ...
- [图论]最大流问题(Maximum flow)的定义
首先定义网络(network)N =(V,E), V表示顶点(Vertices)集合, E表示边(Edges)集合. s,t是V中的两个顶点,分别表示网络N中的源点(source)和汇点(sink). ...
- 一个数组保存了N个结构,每个结构保存了一个坐标,结构间的坐标都不相同,请问如何找到指定坐标的结构(除了遍历整个数组,是否有更好的办法)?
#include <iostream> #include <map> using namespace std; #define N 5 typedef struct point ...
- studio中碰到的jni问题:java.lang.UnsatisfiedLinkError
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52606328 studio中碰到 ...
- HDFS:NameNode、DataNode、SecondaryNameNode
可以一句话描述 HDFS:把客户端的大文件存放在很多节点的数据块中. HDFS设计原则: 1,文件以块(block)方式存储: 2,通过副本机制提高可靠度和读取吞吐量: 3,每个区块至少分到三台Dat ...
- Java继承时的初始化顺序
Java程序在启动和运行时,需要首先完成初始化的工作.在涉及到继承.static成员变量等因素时,初始化的顺序就复杂起来.下面以一个例子说明继承时的Java初始化顺序. 例子: class Insec ...
- cassandra 如何写数据以及放置副本
application发送数据到server application 发送请求到server 根据设置的load balance 规则从cluster中挑选一个coordinator,一般使用轮询即可 ...
- nfc开发
很多Android设备已经支持NFC(近距离无线通讯技术)了.本文就以实例的方式,为大家介绍如何在Android系统中进行NFC开发. Android NFC开发环境 使用硬件:Google Nexu ...
- 1073. Scientific Notation (20)
题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ...