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的更多相关文章

  1. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

  2. 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 ...

  3. 【Lintcode】104.Merge k Sorted Lists

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  4. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  5. [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 ...

  6. 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 ...

  7. No.023:Merge k Sorted Lists

    问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  8. 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 ...

  9. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

随机推荐

  1. lucene api

    设置重新打开索引目录(清空) IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer());conf.setOpen ...

  2. docker从零开始 存储(五)存储驱动介绍

    关于存储驱动程序 要有效地使用存储驱动程序,了解Docker如何构建和存储镜像以及容器如何使用这些镜像非常重要.您可以使用此信息做出明智的选择,以确定从应用程序中保留数据的最佳方法,并避免在此过程中出 ...

  3. Android6.0获取运行时权限

    照着<第一行代码>打代码,然并卵,感叹技术进步的神速.最后提醒一点:IT类的书籍一定要注意出版时间!出版时间!出版时间!重要的事情说三遍 问题出在android6.0的权限获取问题上,以前 ...

  4. Android 使用WebView控件展示SVG图

    1.添加布局界面代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  5. XAMPP配置vhosts多站点/绝对正确

    XAMPP有时候你需要一些顶级域名访问方式来访问你本地的项目也就是虚拟主机配置,这时候就需要配置虚拟主机,给你的目录绑定一个域名,实现多域名绑定访问. 在Mac 下一直使用 MAMP 搭建本地 php ...

  6. CF 990A. Commentary Boxes【数学/模拟】

    [链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...

  7. 1957 Problem F 乌鸦坐飞机

    #include <iostream> #include <algorithm> #include <cmath> #include <vector> ...

  8. 最小生成树 (Minimum Spanning Tree,MST) --- Prim算法

    本文链接:http://www.cnblogs.com/Ash-ly/p/5409904.html 普瑞姆(Prim)算法: 假设N = (V, {E})是连通网,TE是N上最小生成树边的集合,U是是 ...

  9. log4j2+slf4j+junit

    dependencies: compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21' compile group: 'org.a ...

  10. POJ1300Door Man(欧拉回路)

                                                               Door Man Time Limit: 1000MS   Memory Limi ...