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.
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode p1 = l1;
ListNode p2 = l2; ListNode fakeHead = new ListNode(0);
ListNode p = fakeHead; while(p1 != null && p2 != null){
if(p1.val <= p2.val){
p.next = p1;
p1 = p1.next;
}else{
p.next = p2;
p2 = p2.next;
} p = p.next;
} if(p1 != null)
p.next = p1;
if(p2 != null)
p.next = p2; return fakeHead.next;
}
重点是next边界的使用,注意一点 最好使用新指针 不要改动原有的东西。
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 (合并两个有序链表)
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 ...
- [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 ...
随机推荐
- JavaScript中的splice方法
splice方法根据传入的不同参数可分别实现删除和插入操作 使用splice(pra1,pra2,pra3)方法,需要为其提供如下参数: 1.pra1为其起始索引(即希望开始添加元素的地方) 2.pr ...
- cocos2d-x的CCAffineTransform相关变换实现原理
稍有opengl或3d基础的都知道平移/旋转/缩放这几个基本模型视图变换的实现原理, 最近看了下cocos2d-x相关部分的实现, 了解了这些实现那些各种坐标变换基本不在话下了, cocos2d-x本 ...
- 使用 Sublime Text 2 开发 Unity3D 项目
用 Sublime 已经有很长一段时间,很舒适,很贴心,根本停不下来.之前因为是开发页游,所以是用 AS3 开发,近段时间,新开了个手游项目,引擎方面选定了 Unity3D,老实说,之前没有太多的 3 ...
- Ajax&Java
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML) 是一种基于浏览器的XMLHttpRequest对象实现的创建交互式网页应用的网页开发技 ...
- Create and Install Timer Job in MOSS 2007
Excute Timerjob public class TriggerLoadCacheTimerJob : SPJobDefinition { string ExceptionFlag = str ...
- Android genymotion安装输入法不兼容
genymotion 此应用与您的手机不兼容 http://blog.csdn.net/caiwenfeng_for_23/article/details/41692923
- 数据类型和Json格式(转载)
作者: 阮一峰 日期: 2009年5月30日 1. 前几天,我才知道有一种简化的数据交换格式,叫做yaml. 我翻了一遍它的文档,看懂的地方不多,但是有一句话令我茅塞顿开. 它说,从结构上看,所有的数 ...
- 搭建Hadoop2.6.4伪分布式
准备工作 操作系统 CentOS 7 软件环境 JDK 1.7.0_79 下载地址 SSH,正常来说是系统自带的,若没有请自行搜索安装方法 关闭防火墙 systemctl stop firewalld ...
- 15个实用的jQuery代码片
(function($) { var cache = []; // Arguments are image paths relative to the current page. $.preLoadI ...
- javascript运算符与表达式
表达式 表达式是关键字.运算符.变量以及文字的组合,用来生成字符串.数字或对象.一个表达式可以完成计算.处理字符.调用函数.或者验证数据等操作. 表达式的值是表达式运算的结果,常量表达式的值就是常量本 ...