leetcode: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.
分析:思路比较简单,遍历两个有序链表,每次指向最小值。
code如下:
/**
* 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 *h = new ListNode(0), *p = h;
while(l1 != NULL && l2 != NULL)
{
if(l1 -> val < l2 -> val)
{
p -> next = l1;
l1 = l1 -> next;
}
else
{
p -> next = l2;
l2 = l2 -> next;
}
p = p -> next;
}
if(l1 != NULL)
p -> next = l1;
if(l2 != NULL)
p -> next = l2;
return h -> next;
}
};
leetcode:Merge Two Sorted Lists(有序链表的归并)的更多相关文章
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] 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] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- 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: Merge Two Sorted Lists 解题报告
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
- LeetCode Merge k Sorted Lists (链表)
题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- [Leetcode] 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:Merge k Sorted Lists
题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
随机推荐
- Android系统Recovery工作原理
Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作 http://blog.csdn.net/mu0206mu/article/d ...
- AutoResetEvent 运用
static AutoResetEvent are = new AutoResetEvent(true);//初始化为开 static void Main(string[] args) { //如果这 ...
- in_array函数的第三个参数 strict
看段代码 <?php $array = array('testing',0,'name'); var_dump($array); var_dump(in_array('foo', $array) ...
- 理解 Memory barrier
理解 Memory barrier(内存屏障) 发布于 2014 年 04 月 21 日2014 年 05 月 15 日 作者 name5566 参考文献列表:http://en.wikipedia. ...
- HDU 4569 Special equations(数学推论)
题目 //想不出来,看了解题报告 /* 题意:给你一个最高幂为4的一元多项式,让你求出一个x使其结果模p*p为0. 题解:f(x)%(p*p)=0那么一定有f(x)%p=0,f(x)%p=0那么一定有 ...
- iOS视频压缩
// // ViewController.m // iOS视频测试 // // Created by apple on 15/8/19. // Copyright (c) 2015年 tqh. All ...
- UITableView多选全选
自定义cell和取到相应的cell就行了 TableViewCell.h #import <UIKit/UIKit.h> @interface TableViewCell : UITabl ...
- AwSnap:让全版本(Windows、iOS、Android)Chrome浏览器崩溃的有趣漏洞
彩蛋爆料直击现场 几周前,我们曾报道了13个字符导致Chrome崩溃的漏洞.然而,这个漏洞有个小小的遗憾,那就是它只在MAC OS X下生效,其他系统并不受影响. 现在,我们又有了一个更有趣的漏洞.黑 ...
- SQL技术内幕-6 rank()over(order by XX COLLATE) 的用法
DECLARE @Names TABLE ( name VARCHAR(20) ); INSERT INTO @Names VALUES ('DeSzmetch'),('DESZMETCH'),('D ...
- iOS开发之都兴忱小结
1.NSArray/NSDictionary ------> strong temp和self.arr是同一地址. 2.NSArray/NSDictionary ------->copy ...