LintCode - Merge Two Sorted List
LintCode - Merge Two Sorted Lists
Web Link
http://www.lintcode.com/en/problem/merge-two-sorted-lists/
Description
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.
Example
Given 1->3->8->11->15->null, 2->null , return 1->2->3->8->11->15->null.
Code - C++
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param ListNode l1 is the head of the linked list
* @param ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// write your code here
if (l1 == NULL) {
return l2;
}
if (l2 == NULL) {
return l1;
}
ListNode* head = l1->val < l2->val ? l1:l2;
ListNode* temp = new ListNode(0);
while (l1 != NULL && l2 != NULL) {
if (l1->val < l2->val) {
temp->next = l1;
temp = l1;
l1 = l1->next;
} else {
temp->next = l2;
temp = l2;
l2 = l2->next;
}
}
if (l1 == NULL) {
temp->next = l2;
}
if (l2 == NULL) {
temp->next = l1;
}
return head;
}
};
Tips
None
LintCode - Merge Two Sorted List的更多相关文章
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- 165. Merge Two Sorted Lists【LintCode by java】
Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...
- 【Lintcode】104.Merge k Sorted Lists
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- [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 ...
- 21. 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 ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- No.021:Merge Two Sorted Lists
问题: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
随机推荐
- eclipse出现build path 错误
右击本项目-build path-config build path-libraries-发现有选项是带错误符号,于是点击edit然后点击alternative jre选择安装了的jre就解决问题了
- 微信小程序自动去除input空格的方法
当用户输入账号或者密码的时候,可能会有输入空格的情况,但是实际需要是不能够有空格的,所以我们要做一个去除空格,并且适应所有input的name参数的方法,下面请看源码: wxml: <input ...
- set注入
顾名思义set注入必须要有set方法. 基本类型的注入.引用类型注入.List注入.Set注入.Map注入.Properties注入 public class person { private car ...
- 输入数字n,按顺序打印出从1到最大的n位十进制数
题目:输入数字n,按顺序打印出从1到最大的n位十进制数.比如输入3,则打印出1,2,3一直到最大的999. 跳进面试官的陷阱 void PrintfToMaxNDigits(int n) { ; ; ...
- python+selenium 组织用例方式 总结
1.unittest.main() 将一个单元测试模块变为可直接运行的测试脚本,main()方法使用TestLoader类来搜索所有包含在该模块中以“test”命名开头的测试方法,并自动执行他们.执行 ...
- BZOJ 1036: [ZJOI2008]树的统计Count-树链剖分(点权)(单点更新、路径节点最值、路径求和)模板,超级认真写了注释啊啊啊
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 23015 Solved: 9336[Submit ...
- Java的Hashtable类(转)
文章来源:http://blog.csdn.net/zhna123_2011/article/details/6741479 ps:直接copy 哈希表是一种重要的存储方式,也是一种常见的检索方法.其 ...
- flutter 快捷键
1.热重载 alt+\ 2.热重启 alt+shift+\ 3.快速生成模板 stf 直接生成有状态模板 4.模拟器中文输入法 http://www.mdpda.com/app/apk3670941. ...
- POJ 2387 链式前向星下的SPFA
(POJ)[http://poj.org/problem?id=2387] Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- MySQL 将某个字段值的记录排在最后,其余记录单独排序
1.按 status 值 2 5 3 的顺序排序,值相同则按修改时间排序 order by FIELD(status,2,5,3),a.ModifyTime desc 2.将 status = 3 的 ...