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. grep 所有多个关键字

    标签(空格分隔): Linux 多个关键字 或 关系 egrep 'CommentManager|getComment' --color catalina.log.2017-03-15 grep -E ...

  2. admin管理工具

    admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLE ...

  3. hdu 5176(并查集)

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  4. hdu 2196(方法1:经典树形DP+方法2:树的直径)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. Cannot open include file: 'libxml/xpath.h': No such file or directory

    在搭建scrapy爬虫框架时,通过pip安装lxml遇到了这个问题,我是用32位的windows搭建爬虫框架,python版本是2.7.12, 解决方案如下: 原因: 在网上各种找原因,有大神是说没安 ...

  6. Django+ openpyxl 导出文件,设置表头/内容格式

    之前使用xlrd.xlrt处理文件的导入导出,这两个主要用于excel2003格式的文件的读写,并且xlrt最多可以写入256行,大于256行会报错 找了新插件openpyxl,对它找到针对某一行设置 ...

  7. Django 在 view 中使用 Paginator分页插件

    1.在 view 中使用 Paginator def query(request,sql): # 创建连接 connection = pymysql.connect(**config) try: wi ...

  8. [thinkphp] ajaxReturn案例

    javascript: <script> $('.ajax-post').click(function(){ var action_url=$('form').attr('action') ...

  9. [xampp] ubuntu终端连接xampp的mysql

    /opt/lampp/bin/mysql -u root

  10. ( 转 ) Mysql group_concat 的反向应用实现(Mysql列转行)

    用过Mysql的都知道她有一个很好的实现行转列功能的函数group_concat函数,非常方便 点击(此处)折叠或打开 SELECT * FROM group_test; SELECT id, GRO ...