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.

Have you met this question in a real interview?

Yes
Example

Given 1->3->8->11->15->null2->null , return 1->2->3->8->11->15->null.

LeetCode上的原题,请参见我之前的博客Merge Two Sorted Lists

解法一:

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) {
ListNode *dummy = new ListNode(-), *cur = dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
cur->next = 1l;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 ? l1 : l2;
return dummy->next;
}
};

解法二:

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) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};

解法三:

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) {
if (!l1) return l2;
if (!l2) return l1;
ListNode *head = (l1->val < l2->val) ? l1 : l2;
ListNode *nonHead = (l1->val < l2->val) ? l2 : l1;
head->next = mergeTwoLists(head->next, nonHead);
return head;
}
};

解法四:

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) {
if (!l1 || (l2 && l1->val > l2->val)) swap(l1, l2);
if (l1) l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
};

[LintCode] Merge Two Sorted Lists 混合插入有序链表的更多相关文章

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

  2. [LeetCode] 21. 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 ...

  3. 21. Merge Two Sorted Lists (Java 合并有序链表 空间复杂度O(1))

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  4. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  5. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  6. [leetcode]21. 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 ...

  7. LintCode - Merge Two Sorted List

    LintCode - Merge Two Sorted Lists LintCode - Merge Two Sorted Lists Web Link Description Code - C Ti ...

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

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

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

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

随机推荐

  1. Spark on YARN的部署

    Spark on YARN的原理就是依靠yarn来调度Spark,比默认的Spark运行模式性能要好的多,前提是首先部署好hadoop HDFS并且运行在yarn上,然后就可以开始部署spark on ...

  2. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

  3. 关于java中多态的理解

    java三大特性:封装,继承,多态. 多态是java的非常重要的一个特性: 那么问题来了:什么是多态呢? 定义:指允许不同类的对象对同一消息做出响应.即同一消息可以根据发送对象的不同而采用多种不同的行 ...

  4. *** $CI =& get_instance() 用法:关于CodeIgniter中get_instance() 函数

    使用场景: 注意 get_instance 的使用场景,这个方法并不是用在控制器中的.而是用在控制器外面,比如类库中,想操作 CI 超级对象的时候,超级对象实际上就是当前控制器的实例. 你随便下个CI ...

  5. 使用静态函数impl模式做接口

    使用静态函数impl模式做接口 impl即桥接模式,主要是为了隐藏数据和减少不必要的编译. 普通的impl模式做接口一般是: A类是接口,B类继承A类,是A类的实现,C类,包含A类和B类的头文件,把B ...

  6. 在Mac中像Windows一样查看Tomcat控制台信息

    在Windows系统中,通过startup.bat启动Tomcat之后会打开一个控制台,输出日志信息,在系统调试过程中,也会随时输入日志或错误信息,对开发很有帮助. 在Mac中,通过startup.s ...

  7. TMS320F28027/26/23/22/21/20芯片解密单片机破解原理!

    TMS320F28027/26/23/22/21/20芯片解密单片机破解 TMS320F2802系列芯片解密型号: TMS320F28027F.TMS320F280270.TMS320F28027.T ...

  8. 各大IT技术博客排行榜

    cnblogs 积分排名前3000名 http://www.cnblogs.com/ 左侧有推荐博客排行 cppblog http://www.cppblog.com/AllBloggers.aspx ...

  9. Idea在src下不能编译XML文件

    IDEA编译XML文件,如果需要在src下编译就需要在maven配置中加如下配置: <build> <finalName>SpringDemo</finalName> ...

  10. URAL 2089 Experienced coach Twosat

    Description Misha trains several ACM teams at the university. He is an experienced coach, and he doe ...