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 together the nodes of the first two lists.
思路.首先判断是否有空链表,如果有,则直接返回另一个链表,如果没有,则开始比较两个链表的当前节点,返回较小的元素作为前驱,并且指针向后移动一位,再进行比较,如此循环,知道一个链表的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(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(l2->next, l1);
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 = (l1->val > l2->val)?l2:l1;
ListNode* temp;
while(l1!=NULL && l2!=NULL)
{
while(l1->next!=NULL&&l2!= NULL &&l1->next->val <= l2->val)
{
l1 = l1->next;
}
if(l1 != NULL&&l2 !=NULL &&l1->val <= l2->val)
{
temp = l1->next;
l1->next = l2;
l1 = temp;
}
while(l2->next !=NULL&& l1 !=NULL &&l2->next->val <= l1->val)
{
l2 = l2->next;
}
if(l2 != NULL &&l1 != NULL&&l2->val <= l1->val)
{
temp = l2->next;
l2->next = l1;
l2 = temp;
} }
return head;
}
};
LeetCode 【21. Merge Two Sorted Lists】的更多相关文章
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 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 ...
- Leetcode练习题21. Merge Two Sorted Lists
题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode LinkList 23. Merge k Sorted Lists
这两天一直也没有顾上记录一下自己做过的题目,回头看看,感觉忘的好快,今天做了一个hard,刚开始觉得挺难得,想了两种方法,一种是每次都从k个list中选取最小的一个,为空的直接跳过,再就是每次合并其中 ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
随机推荐
- sbrk与brk的使用小例子
sbrk() 和 brk() - Unix的系统函数 sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位. void* ...
- java中重载、覆盖和隐藏三者的区别分析
重载:方法名相同,但参数不同的多个同名函数 注意:1.参数不同的意思是参数类型.参数个数.参数顺序至少有一个不同 2.返回值和异常以及访问修饰符,不能作为重载的条件(因为对于匿名调用,会出现歧义,eg ...
- vm10虚拟机安装Mac OS X10.10教程[转]
update:http://www.sysprobs.com/vmware-workstation-8-0-8-0-1-unlocker-to-run-mac-os-x-guest-in-window ...
- eclipse配置ros cakin编译环境
先安装eclipse,之前的博客:http://www.cnblogs.com/CZM-/p/5942435.html Catkin-y 方法使用catkin无法make eclipse工程,生成pr ...
- OC测试错误整理
3. NSDictionary *dict = [NSDictionary dictionaryWithObject:@"a value" forKey:@"aKey&q ...
- JavaScript本地对象 内置对象 宿主对象
在ECMAScript中,所有对象并非同等创建的. 一般来说,可以创建并使用的对象有3种:本地对象.内置对象和宿主对象. 1. 本地对象 ECMA-262把本地对象(native obje ...
- IIS调试技巧
VS2010如何调试IIS上的网站 通常,我们在Visual Studio里调试ASP.NET网站,都是加个断点,然后按F5,在VS自带的虚拟服务器下调试的.但有时候,VS自带的服务器弱爆了,无法满足 ...
- 在Eclipse上建立hadoop2.2.0/hadoop2.4.0源代码阅读环境
1.安装依赖的包: yum install gcc-c++ g++ autoconf automake libtool cmake zlib1g-dev pkg-config libssl-dev 2 ...
- 基于百度定位及天气获取的DEMO +fragment+sharedpreference
此工程较BaiduLocationXML相比:1.植入fragment,结合微信UI2.在原本主界面的button textview textview 移植到Fragment13.增加网络判断,网 ...
- Nginx反向代理关于端口的问题
Nginx反向代理关于端口的问题 http://www.cnblogs.com/likehua/p/4056625.html Nginx默认反向后的端口为80,因此存在被代理后的端口为80的问 ...