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 ...
随机推荐
- 【LA3461】Leonardo的笔记本
白书例题. #include<bits/stdc++.h> using namespace std; ],vis[],cnt[]; inline int read(){ ,x=;char ...
- 安装cuda8.0时出现错误:Missing recommended library: libGLU.so Missing recommended library: libGL.so
安装cuda8.0时出现错误: Missing recommended library: libGLU.soMissing recommended library: libGL.so Installi ...
- IOS UITableViewUITableView小技巧--实现cell向左滑动删除,编辑等功能
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...
- dms程序调试
Q:程序编译通过后,出现tomcat 端口被占用问题 参考 http://www.cnblogs.com/HoverM/p/3872163.html 先使用netstat -ano|findstr ...
- [转]CreateDIBitmap与CreateDIBSection
首先明确最主要区别:CreateDIBitmap创建的是设备相关位图句柄 - HBITMAP. CreateDIBSection创建的是设备 ...
- Python 进阶 之 socket模块
Python Socket 编程详细介绍 转载地址:https://gist.github.com/kevinkindom/108ffd675cb9253f8f71?signup=true Pytho ...
- PHP serialize 和 JSON 解析与区别
PHP serialize 和 JSON 解析与区别 对于JSON(JavaScript Object Notation)大家应该不陌生,它是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器 ...
- 【互动问答分享】第7期决胜云计算大数据时代Spark亚太研究院公益大讲堂
“决胜云计算大数据时代” Spark亚太研究院100期公益大讲堂 [第7期互动问答分享] Q1:Spark中的RDD到底是什么? RDD是Spark的核心抽象,可以把RDD看做“分布式函数编程语言”. ...
- Python 读写 Excel(转)
Python 读写 Excel 基本上, 这个网页已经说明一切了: http://pypi.python.org/pypi/xlrd 等有时间再把这个页面写漂亮,现在先记一些代码. 读Excel 先建 ...
- HDU 4528 小明系列故事――捉迷藏
广搜. 根据题意,可以知道状态总共有$4*n*m$种.每一个位置四种状态:两个都没有发现:发现$E$没发现$D$:发现$D$没发现$E$:两个都发现. 每次移动的花费都是$1$,队列里面状态的费用是单 ...