题目

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.

翻译

合并两个有序的链表

Hints

Related Topics: LinkedList

参考 归并排序-维基百科,可以递归也可以迭代,基本的链表操作

代码

Java

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
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;
}
}
}

Python

//recursively
class Solution(object):
def mergeTwoLists(self, l1, l2):
if l1==None: return l2;
if l2==None: return l1; if l1.val<l2.val:
l1.next = self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next = self.mergeTwoLists(l1,l2.next)
return l2 //iteratively
class Solution(object):
def mergeTwoLists(self, l1, l2):
dummy = curr = ListNode(0) while l1 and l2:
if l1.val<l2.val:
curr.next = l1
l1 = l1.next
else:
curr.next = l2
l2 = l2.next
curr = curr.next
curr.next = l1 or l2
return dummy.next

# 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

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

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

  3. 21. Merge Two Sorted Lists【easy】

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

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

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

  6. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

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

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

  9. Java [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 spli ...

随机推荐

  1. java 计算百分数方法

    俗话说好记性不如烂笔头,故记之. DecimalFormat decimalFormat = new DecimalFormat("##.00%"); System.out.pri ...

  2. JS日历控件优化(增加时分秒)

    JS日历控件优化      在今年7月份时候 写了一篇关于 "JS日历控件" 的文章 , 当时只支持 年月日 的日历控件,现在优化如下:      1. 在原基础上 支持 yyyy ...

  3. QT的常用对话框的应用

    QMessageBox类提供了常用的弹出式对话框:提示.警告.错误.询问.关于对话框 需要添加头文件 #include <QMessageBox> MESSAGE  是要是显示的字符串 v ...

  4. linux 文件夹和文件操作

    1.统计目录有多少个文件数 find ./company -type f | wc -l 2.删除文件夹中的文件 rm -f * #最经典的方法,删除当前目录下的所有类型的文件 rsync --del ...

  5. “System.Reflection.AmbiguousMatchException”类型的异常在 mscorlib.dll 中发生

    错误提示: “System.Reflection.AmbiguousMatchException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理. 发现不明确的匹配. 问题原 ...

  6. 学会如何使用Github进行托管代码和用markdown撰写心得

    这次作业是学会如何使用Github进行托管代码和用markdown撰写心得. 1.掌握使用Git进行代码版本,使用github进行代码托管.(git使用,推荐imooc公开课: 公开课 ) 登录 gi ...

  7. Android导入AS工程

     AS  导入工程 还得 新建工程贴代码 

  8. 在AspNetCore 中 使用Redis实现分布式缓存 (转载)

    文章概念描述 分布式缓存描述:分布式缓存重点是在分布式上,相信大家接触过的分布式有很多中,像分布式开发,分布式部署,分布式锁.事物.系统 等有很多.使我们对分布式本身就有一个很明确的认识,分布式就是有 ...

  9. 大数据入门第十二天——sqoop入门

    一.概述 1.sqoop是什么 从其官网:http://sqoop.apache.org/ Apache Sqoop(TM) is a tool designed for efficiently tr ...

  10. 20155321 《网络攻防》 Exp7 网络欺诈防范

    20155321 <网络攻防> Exp7 网络欺诈防范 实验内容 简单应用SET工具建立冒名网站 因为钓鱼网站是在本机的http服务下使用,因此需要将SET工具的访问端口改为http默认的 ...