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 ...
随机推荐
- SPSS数据分析-时间序列模型
我们在分析数据时,经常会碰到一种数据,它是由时间累积起来的,并按照时间顺序排列的一系列观测值,我们称为时间序列,它有点类似于重复测量数据,但是区别在于重复测量数据的时间点不会很多,而时间序列的时间点非 ...
- IDE编辑器编码配置
做跨平台开发时,大家用不同的IDE合作开发,最令人头疼的事就是各种乱码问题. 常用的IDE都支持utf-8编码和unix格式行尾'\n'. 1.XCODE设置文本编码及换行Xcode >> ...
- db2命令
把远程的数据库信息加载到本地 第一步,catalog server端的node ,命令如下: db2 catalog tcpip node db2node remote hostname server ...
- qml中打开本地html
main.cpp QString tmploc = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); QDi ...
- Scala 学习笔记(五)
def main(args : Array[String]): Unit = { def add(x:Int,y:Int):Int = { return x+y; } def subtract:(In ...
- OC--编码建议
原文 http://www.cocoachina.com/ios/20151118/14242.html 本文是投稿文章,作者: IOS_Tips(微信公众号) “神在细节之中” Object ...
- 【 2013 Multi-University Training Contest 2 】
HDU 4611 Balls Rearrangement 令lcm=LCM(a,b),gcd=GCD(a,b).cal(n,a,b)表示sum(abs(i%a-i%b)),0<=i<n. ...
- C#简易一元二次求解器
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- Coding源码学习第四部分(Masonry介绍与使用(二))
接上篇,本篇继续对Masonry 进行学习,接上篇示例: (6)Masonry 布局实现iOS 计算器 - (void)exp4 { WS(weakSelf); // 申明区域,displayView ...
- centos 6.6编译安装git-2.7.0 最新git编译安装
系统环境:centos6.6 1,安装准备 yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gettext-deve ...