21. Merge Two Sorted Lists【easy】

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

解法一:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == NULL || l2 == NULL) {
return l1 ? l1 : l2;
} ListNode * dummy = new ListNode(INT_MIN);
ListNode * temp = dummy; while (l1 && l2) {
if (l1->val > l2->val) {
dummy->next = l2;
l2 = l2->next;
}
else {
dummy->next = l1;
l1 = l1->next;
} dummy = dummy->next;
} if (l1 || l2) {
dummy->next = l1 ? l1 : l2;
} return temp->next;
}
};

由于最后是弄到list1中,但是我们不知道list1还是list2的第一个元素关系,最后结果的list1中的头结点可能会改变,所以需要引入dummy节点。

解法二:

 public ListNode mergeTwoLists(ListNode l1, ListNode l2){
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}

参考了@yangliguang 的代码

解法三:

 public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1; ListNode handler;
if(l1.val < l2.val) {
handler = l1;
handler.next = mergeTwoLists(l1.next, l2);
} else {
handler = l2;
handler.next = mergeTwoLists(l1, l2.next);
} return handler;
}
}

参考了@RunRunCode 的代码

解法二和解法三都是递归,还没有完全弄明白……

21. Merge Two Sorted Lists【easy】的更多相关文章

  1. Leetcode 21. Merge Two Sorted Lists(easy)

    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 [Difficulty: Easy]

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

  3. 两个升序链表的合并 Merge Two Sorted Lists 【 leetcode】

    class Solution {public:   ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {   ListNode *p; ListN ...

  4. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

  5. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  6. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  7. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  8. 21. Merge Two Sorted Lists(合并2个有序链表)

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  9. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

随机推荐

  1. boost 1.57 vs2013 编译

    启动vs2013中的命令行注意区分32/64, 进入boost目录,  再次运行 bootstrap.bat 编译: bjam.exe stage --toolset=msvc-12.0  --sta ...

  2. Java小问题的解决方法系列

    1)IDEA中文乱码,解决方法:http://blog.csdn.net/zht666/article/details/8953516 2)卸载OpenJdk,http://my.oschina.ne ...

  3. Scala实战高手****第15课:Scala类型参数编程实战及Spark源码鉴赏

    1.Scala的类和方法.函数都可以是泛型 2.上界:表示泛型的类型必须是某种类型或者其类型的子类,语法:<: ,对类型进行限定 3.下界:表示泛型的类型必须是某种类型或者其类型的父类,语法:& ...

  4. iOS开发-设置在使用NavigateController时View的顶部位置

      最近我在开发中遇到了一个问题,在使用NavigationController时内部的ViewController的View总是与屏幕顶部对齐,而我们有时候不需要这种效果: 在开发过程中,我们可能会 ...

  5. Swift中混编OC第三方库

    现在Swift的第三方库还比较少,有时候需要使用OC的第三方库,其实也是很容易的.   我们使用如下步骤: 1.新建的Swift项目,第一次创建OC文件时会询问是否生成 桥接头,选择是的话会生成一个桥 ...

  6. 阿里云(ECS)Linux客户端SSH会话连接超时OperationTimedOut

    问题描述:使用SecureCRT等SSH客户端连接Linux服务器时,提示Operation timed out. 问题原因:SSH服务未配置或注释掉向SSH客户端连接会话发送频率和时间. 解决方法: ...

  7. Git服务器分类

    目录(?)[-] 服务器上的 Git 协议 本地协议 优点 缺点 SSH 协议 优点 缺点 Git 协议 优点 缺点 HTTPS 协议 优点 缺点 在服务器部署 Git 将纯目录转移到服务器 小型安装 ...

  8. mahout运行测试与数据挖掘算法之聚类分析(一)kmeans算法解析

    在使用mahout之前要安装并启动hadoop集群 将mahout的包上传至linux中并解压即可 mahout下载地址: 点击打开链接 mahout中的算法大致可以分为三大类: 聚类,协同过滤和分类 ...

  9. 有哪些通俗易懂的例子可以解释 IaaS、PaaS、SaaS 的区别?

    有哪些通俗易懂的例子可以解释 IaaS.PaaS.SaaS 的区别? S 软件 P 中间件 I 基础设施

  10. windowsclient开发--使用、屏蔽一些快捷键

    每一个windowsclient都有自己的一些快捷键,有的是windows系统提供的. 今天就要与大家分享一下.在windowsclient开发过程中对按键的处理. ESC按键 Duilib这个库中, ...