Leetcode 21 Merge Two Sorted Lists 链表
合并两个已排序的链表,考到烂得不能再烂的经典题,但是很多人写这段代码会有这样或那样的问题
这里我给出了我的C++算法实现
/**
* 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) {
if(l1 && !l2) return l1;
if(l2 && !l1) return l2;
if( !l2 && !l1) return NULL; //保证所有列表不为空
ListNode* t1 = l1;
ListNode* t2 = l2;
ListNode* t = NULL;
if(t1->val < t2->val){ //确定表头t是l1还是l2
t = t1;
t1 = t1->next;
}
else{
t = t2;
t2 = t2->next;
}
for(;t1&&t2; t = t->next){//确定表头t的下一个元素
if(t1->val < t2->val){
t->next = t1;
t1 = t1->next;
}
else{
t->next = t2;
t2 = t2->next;
}
}
if(t1){//t1不为空,将l1剩余部分插入到t后
t->next = t1;
}
if(t2){//t2不为空,将l2剩余部分插入到t后
t->next = t2;
}
if(l1->val < l2->val) return l1;
else return l2;//确定表头
}
};
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 ...
- [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合并两个有序链表 (C++)
题目: 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 ...
- 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 ...
随机推荐
- iOS开发-xcdatamodeld文件 CoreData的介绍和使用,sqlite的使用
CoreData的介绍和使用 源引:http://www.jianshu.com/p/d027090af00e CoreData是数据存储的一种方式,CoreData实质也是对SQLite的封装. ...
- 5.HotSpot的算法实现
1.枚举根节点 在可达性分析中,可以作为GC Roots的节点有很多,但是现在很多应用仅仅方法区就有上百MB,如果逐个检查的话,效率就会变得不可接受. 而且,可达性分析必须在一个一致性的快照中进行-即 ...
- ios8以后,使用UIAlertViw时pop/push页面后,键盘闪一下的问题
代码为 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"感谢你对我们提出的意见或 ...
- 交换机和VLAN
交换机 交换机的两个作用:一是维护CAM表,CAM表是计算机的MAC地址和交换机端口的映射表:另一个是根据CAM来进行数据的转发. 以太网交换机转发数据帧有三种方式: 1.存储转发:即先存储后转发的方 ...
- 2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- Ubuntu系统监控cpu memery 磁盘Io次数 IO速率 网卡 运行时间等信息的采集
实验室最近在做的项目要做ubuntu系统监控,要获得系统的一些信息并返回给web服务器. web服务器与ubuntu主机的通信我写的程序用的是socket,至于为什么不用java程序ssh到对应的主机 ...
- LeetCode OJ--Swap Nodes in Pairs
https://oj.leetcode.com/problems/swap-nodes-in-pairs/ 链表的处理 /** * Definition for singly-linked list. ...
- select 练习4
21.查询score中选学多门课程的同学中分数不是所有成绩中最高分成绩的记录. select * from score where cno in(select cno from score grou ...
- JS-定时器管理实例
/** * Created by 12461 on 2016/11/6. */window.onload = function () { var oBtn1 = document.getElement ...
- SQL基础语法等
--1.while循环 declare @sum int declare @i int ) begin set @sum =@sum+@i ) print @i end print @sum --2. ...