【Merge Two Sorted Lists】cpp
题目:
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.
代码:
/**
* 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) {
ListNode dummy(-);
ListNode *p = &dummy;
while ( l1 && l2 )
{
if ( l1->val<l2->val )
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
p->next = l1 ? l1 : l2;
return dummy.next;
}
};
tips:
无。
========================================
第二次过这道题,l1或l2为空之后,不用一个一个链接了,可以直接补上剩下的linked list。
/**
* 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) {
ListNode head();
ListNode* p = &head;
while ( l1 && l2 )
{
if ( l1->val < l2->val )
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
p->next = l1 ? l1 : l2;
return head.next;
}
};
【Merge Two Sorted Lists】cpp的更多相关文章
- 【Merge K Sorted Lists】cpp
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- leetcode 【 Merge Two Sorted Lists 】 python 实现
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- leetcode 【 Merge k Sorted Lists 】python 实现
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- 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 ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 【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
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【LeetCode】【数组归并】Merge k Sorted Lists
描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
随机推荐
- "/Date(1405056837780)/" 时间转换
//往往json传过来的时间都是"/Date(1405056837780)/" //转换需要的方法 String.prototype.ToString = function (fo ...
- mongodb 修改数据结构的一个例子以及小梳理
mongodb的存储结构是灵活可变的,但是,并不意味着我们就肆意地使用不规则的文档结构.不规则的文档结构对于开发和后期的维护都是一个灾难.所以,还是要有一个约定的格式. 但是,由于前期设计的不周详和其 ...
- php __clone需要注意的问题
当一个对象的属性是另外一个对象时,当有一个对象复制该对象时,当复制到这个属性(一个对象)时,只复制这个属性(对象)的引用,而不复制引用的对象. class Account{ public $bal ...
- python os模块使用方法
os.path模块 basename('文件路径') 去掉目录路径,返回fname文件名 1 import os 2 os.path.basename('/Volumes/1.mp4') ...
- JTable的DefaultModel方法getValueAt(a,row)
行和列都是从0开始索引的,而且不包括netbeans生成的表格头,而是从数据开始,否则就会报错
- hdu 1867 A + B for you again
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1867 A + B for you again Description Generally speaki ...
- hdu 2645 find the nearest station
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2645 find the nearest station Description Since dande ...
- OpenStack:安装Neutron与provider network
1. 安装(1)Install Networking services on a dedicated network node# apt-get install neutron-server neut ...
- [转]论window和Linux之长短
论window和Linux之长短 王垠 http://www.kerneltravel.net/jiqiao/whyLinux.htm — 摈弃 Windows 低效率的工作方式,发掘 Linux 身 ...
- flask环境配置
1.首先要看装python环境,最好是python 2.7版本的: 2.安装easy_install,至今也不确定这个东西是怎么装的,我先下载了一个ez_setup,用命令“python ez_set ...