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 ...
随机推荐
- Android之ImageView 旋转
方案一: (1)获取ImageView对应的图片,并将其转成Bitmap; (2)创建Matrix对象; (3)调用matrix.setRotate( );设置旋转度数 (4)重新创建bitm ...
- Ceph分层存储分析
最近弄Ceph集群考虑要不要加入分层存储 因此花了点时间研究了下 1,首先肯定要弄清Ceph分层存储的结构 ,结构图大概就是下图所示 缓存层(A cache tier)为Ceph客户端提供更好的I/O ...
- boa移植
1.交叉编译 2.复制文件 配置文件boa.conf 移动到/etc/boa/ 目录下 可执行文件boa移动到/usr/sbin/目录下 3.修改配置文件 4.将Linux系统上/etc/mime.t ...
- cocos2d-x源码分析(1)
class CC_DLL CCCopying { public: virtual CCObject* copyWithZone(CCZone* pZone); }; class CC_DLL CCZo ...
- 使用VPN服务器解决公司不能上淘宝的问题
很多公司为了保证员工的效率,通常采用屏蔽端口的方法屏蔽掉了一些网站,比如淘宝.QQ网页版等,这样做虽然也是公司的迫不得已,但是也有点不人性化,毕竟非上班时间也是上不去此类网站的.前些日子电商大站,抢不 ...
- ASP.NET反射(转载)
两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是B型超声波,它可以透过肚皮通过向你体内发射B型超声波,当超声波遇到内脏壁的时 ...
- 怎么把U盘启动改为硬盘启动(适用于U盘安装系统时)
两种方法: 一:安装时: 在自定义创建分区后,如图: 选择系统的启动程序安装的位置,在change device 里设置第一启动装置,和第二启动装置! 二:安装后: 开机未进入系统按F2,进入BIO ...
- mysql数据库查询pdo的用法
最早的php对mysql数据库查询是mysql和mysqli方法,后来php的新版本进一步封住了该方法,于是又pdo,抛开php框架,使用pdo查询数据,使用也是相当简便 <?php ini_s ...
- Python字符转换
Python提供了ord和chr两个内置的函数,用于字符与ASCII码之间的转换. 如:>>> print ord('a') 97 >>> print chr(97 ...
- php笔试题(1)--转载
一份不错的php面试题,附答案,有准备换工作的同学可以参考一下.一.基础题1. 写出如下程序的输出结果 <?php $str1 = null; $str2 = false; ...