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 ...
随机推荐
- SQL Server 监控 使用sp_trace_create
监控前言 上一节我们提到了MSSQL的基于SQL Event的监控,但是有些时候我们需要更加详细.适用于调优排错的监控.SQL Server内部运行的可见性是的查询调整.优化和综合排查成为可能!这一节 ...
- IIS Express 及 vs2008下使用IIS Express
介绍 IIS Express 开发 ASP.NET 的应用程序是我的主要工作.当然我会选择最适合的开发环境.客户多属于企业用户,我的开发的选择,多半是 ASP.NET Web Application ...
- AngularJs学习笔记--expression
原版地址:http://code.angularjs.org/1.0.2/docs/guide/expression 表达式(Expressions)是类Javascript的代码片段,通常放置在绑定 ...
- PHP第一课:开发环境配置
最近在学php,大概了解了一下php的语法结构,以及一些php及基础的知识.由此想到了要亲手试一试:以为以前是学java的用的 ide是myeclipse,所以对eclipse软件布局有特别的钟爱. ...
- CkEditor 插件开发
CKEditor的插件开发其实很简单只需要两步.1.通过CKEditor.plugins.add()方法编写插件的逻辑主体, 2.告诉CKEditor我们有一个自定义插件需要添加进来. //创建插件逻 ...
- Long和Date数据类型之间相互转换代码
static final SimpleDateFormat DATETIME_SEC_STR = new SimpleDateFormat("yyyyMMddHHmmss"); 1 ...
- Cloud Insight支持阿里云一键接入了,so what?
前几天 Cloud Insight 上线了一个新功能,考虑到目前只作为公测,所以只是是悄悄地加了一个接入项,希望你看完这偏文章会有兴趣体验一下. 相信体验过的用户(目前还是个位数)第一感受应该是:这个 ...
- ArrayList和Iterator的用法
import java.util.ArrayList; import java.util.Iterator; public class ArrayListTest { public static vo ...
- iOS-CAEmitterLayer(粒子效果)
扩展:https://github.com/lichtschlag/Dazzle ; , , , ); , ); .f; .f; ; .f; .f; ...
- C# 语音识别(文字to语音、语音to文字)
最近打算研究一下语音识别,但是发现网上很少有C#的完整代码,就把自己的学习心得放上来,和大家分享一下. 下载API: 1)SpeechSDK51.exe (67.0 ...