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 splicing together the nodes of the first two lists.
解题思路:
题目的意思是将两个有序链表合成一个有序链表。
逐个比较加入到新的链表即可。
代码如下:
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode list = new ListNode(0);
ListNode tmp = list;
while (l1 != null || l2 != null) {
if (l1 == null) {
tmp.next = new ListNode(l2.val);
l2 = l2.next;
} else if (l2 == null) {
tmp.next = new ListNode(l1.val);
l1 = l1.next;
} else {
if (l1.val < l2.val) {
tmp.next = new ListNode(l1.val);
l1 = l1.next;
} else {
tmp.next = new ListNode(l2.val);
l2 = l2.next;
}
}
tmp = tmp.next;
}
return list.next;
}
Java [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 ,java
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 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 [Difficulty: Easy]
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
- 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 23]Merge k Sorted Lists
题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...
- [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 ...
随机推荐
- linux set,env和export
set,env和export这三个命令都可以用来显示shell变量 set 显示当前shell的变量,包括当前用户的变量 env 显示当前用户的变量 export 显示当前导出成用户变量的shell变 ...
- MYsqli 绑定插入与查询实例
<?php $conn = new mysqli('localhost','root','','orders'); //连接 $prepare = "insert into t100 ...
- 【BZOJ 1295】 [SCOI2009]最长距离
Description windy有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有的格子含有障碍物. 如果从格子A可以走到格子B,那么两个格子的距离就为两个格子中心的欧几里德距离. 如果从格 ...
- Understanding Responsive Web Design: Cross-browser Compatibility
http://www.sitepoint.com/understanding-responsive-web-design-cross-browser-compatibility/ In the las ...
- python学习笔记18(UliPad 初体验)
在windows下安装配置Ulipad 由于UliPad 是由wxPython 开发的,所以,需要先安装wxPython . wxPython下载地址: http://www.wxpython.org ...
- C#学习笔记---基础入门(三)
泛型<T> 使用泛型能够最大限度的重用代码/保护类型安全,提高性能 泛型成员因为类型的不确定性,不能使用算术运算符/比较运算符 类型参数可以有多个,可以是编译器能够识别的任何类型 类型参数 ...
- Extjs-4.2.1(一)——编辑 hello word
前言:在搭建好环境后,就可以利用Extjs进行开发了. 一.在项目中拷贝相关的Extjs文件,如下图: 注意:不需要整个Ext开发包全部导入,这样很容易造成eclipse卡死,因为eclipse会自动 ...
- replace()替换文字扑获组做法
var txt = "12312131283", str = txt.replace(/(12(.3))/g,"中文$2");//$1是针对前面的扑获组()的如 ...
- 破解之API断点法
上回给大家做的破解教程,地址是http://www.52pojie.net/thread-52719-1-1.html,用的是“调用堆栈”方法.今天给新手提供另一种方法“API函数断点”,这种方法要求 ...
- hdu 1851 A Simple Game 博弈论
简单博弈问题(巴什博弈-Bash Game) 巴什博弈:只有一堆n个物品,两个人轮流从这对物品中取物,规定每次至少取一个,最多取m个,最后取光着得胜. 很容易想到当n%(m+1)!=0时,先取者必胜, ...