[leetcode]_Merge Two Sorted Lists
题目:合并两个有序单链表
思路:一开始想复杂了,以为必须在原链表上修改(绕来绕去还AC了,但是思路相当绕),其实没有,按照正常地合并两个数组同样的方法也对。
代码:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
else if(l2 == null) return l1;
else{
ListNode p = new ListNode(0);
ListNode head = p;
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;
}
while(l1 != null){
p.next = l1;
l1 = l1.next;
p = p.next;
}
while(l2 != null){
p.next = l2;
l2 = l2.next;
p = p.next;
}
return head.next;
}
}
申请一个新链表头head,逐个将较小的元素链到这个链表头的后面,最后返回head.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 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 Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [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
非常easy问题. 唯一的地方可以具有更具挑战是确保不会引入额外的空间.查找开始值最小的名单列表的新掌门人,头从列表中删除.其他操作应该没有问题. class Solution { public: L ...
- 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 ...
- 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 ...
随机推荐
- 苹果safari浏览器登陆时Cookie无法保存的问题
Safari浏览器不支持将非ASCII字符存入Cookie,所以中文在保存的时候就会出问题,分号(";")也不能存在Cookie中,所以需要通过方法去除内容中的分号,在Cookie ...
- java的重载
1.java的重载三种:参数类型不同,构成重载,参数类型不同:::::参数顺序不同构成重载::::::参数个数不同,构成重载
- ListView的position的保持
需求场景: 一个ListView页面,滑动阅读到某一位置,然后退出页面,下次再进入页面的时候,想要直接滑动到上次阅读的位置. 方案1: 页面退出的时候,ListView.getFirstVisible ...
- nginx 添加perl
首先,要知道你原安装的nginx版本,以及原来安装的模块,用nginx -V即可 /usr/sbin/nginx -V 结尾处的--add-module 重新安装时这里可以去掉, 然后去官网下载一个相 ...
- How to Resize a Datafile (文档 ID 1029252.6)
APPLIES TO: Oracle Database - Enterprise Edition - Version 9.2.0.1 and laterInformation in this docu ...
- unique踢出相同元素
unique函数的功能是:去除相邻的重复元素(只保留一个). 函数参数:unique(first,last,compare); //first为容器的首迭代器,last为容器的末迭代器,compare ...
- 翻译:Knockout 快速上手 - 4: 你需要知道的顶级特性
Knockout 最棒的一个特点就是它的可扩展性.Knockout 存在大量的扩展点,包含大量的工具来创建我们的应用程序.许多开发者除了 Knockout 核心库之外没有使用任何其他的脚本库 ( 甚至 ...
- 加链接太麻烦?使用 linkit 模块提升用户编辑体验
在制作网站内容时,适当地添加链接会非常用利于网站内容的SEO.加入链接的文章可以让访客了解到更多相关内容,从而提升文章的质量.被链接到的内容也能因此获得更多的访问和关注.只不过,在内容编辑时添加链接却 ...
- 是否连接VPN
//需要导入ifadds头文件 //是否连接VPN - (BOOL)isVPNConnected{ struct ifaddrs *interfaces = NULL; struct ...
- 洛谷P2738 [USACO4.1]篱笆回路Fence Loops
P2738 [USACO4.1]篱笆回路Fence Loops 11通过 21提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 ...