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. 【bzoj4636】蒟蒻的数列

    由于数据范围过大,直接线段树会炸,离散化或者动态开点都行. 打个标记在树上,最后把树dfs一边算一下即可. #include<bits/stdc++.h> #define N 100000 ...

  2. [NOIP 2015] 斗地主 landlord

    想起几个月之前的 noip2015-只会瞎搞-这道题骗了 30 分.T T 题目 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的 A 到 K 加上大小王的共 54 张牌 ...

  3. struts标签include传参的问题

    传一个常量过去居然为null, <s:include value="/biz/customer/corp/module/franchisemanageright/corpFranchi ...

  4. Selenium2+python自动化55-unittest之装饰器(@classmethod)【转载】

    前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...

  5. Deep Learning关于Vision的Reading List

    最近开始学习深度学习了,加油! 下文转载自:http://blog.sina.com.cn/s/blog_bda0d2f10101fpp4.html 主要是顺着Bengio的PAMI review的文 ...

  6. [图解算法] 归并排序MergeSort——<递归与分治策略>

    #include"iostream.h" void Merge(int c[],int d[],int l,int m,int r){ ,k=l; while((i<=m)& ...

  7. VS中使用Gulp

    关于gulp资料可以访问:http://www.gulpjs.com.cn/,本篇主要讲解在VS中使用gulp对js和css进行压缩合并 1.下载node.js,gulp依赖于node.js,可以访问 ...

  8. Hive知识

    HIVEQL CREATE DATABASE financials(创建数据库) SHOW DATABASES(显示数据库) SHOW TABLES IN 数据库(列出数据库的所有表) SHOW DA ...

  9. HDU 2549 壮志难酬(字符串,处理小数点)

    /* 给你一个小数x,让你算出小数点后第n位是什么,(1 <= n <= 6) Input 首先输入一个t,表示有t组数据,跟着t行: 每行输入一个小数(输入数据保证一定是a.b的形式,为 ...

  10. HDU1754I Hate It(线段树)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...