题目

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. Oracle rdbms Brush password

    Restore database user history account password 1. 用户状态 select * from user_astatus_map; select * from ...

  2. 【转载】python中利用smtplib发送邮件的3中方式 普通/ssl/tls

    #!/usr/bin/python # coding:utf- import smtplib from email.MIMEText import MIMEText from email.Utils ...

  3. 解决Matlab当中for循环运行慢的问题

    做量化操作的时候经常需要使用到matlab编写策略或者计算多因子,for循环非常慢,自己找了一些matlab中for循环的优化方法,for的部分每处理一个大矩阵都要花费大量的时间,这是不可避免需要遇到 ...

  4. jqgrid 获取所有行数据

    如何获取jqgrid所有数据? 通过 getRowData() 方法获得当前行数据 //获取所有行数据,是一个json对象集合 var rowArr= $("#jqGrid").g ...

  5. easyui的datagrid

    datagrid数据的绑定方式: 1)data 后跟数据行的json串 2)url 后跟{"total":,"rows":,"foot":} ...

  6. VBA 上传数据与查找数据 while循环 和 for循环

    Option Explicit  上传数据Private Sub CommandButton1_Click() If MsgBox("请确认数据是否准确,是否确认上传?", vbC ...

  7. # 2017-2018-2 20155319『网络对抗技术』Exp4:恶意代码分析

    2017-2018-2 20155319『网络对抗技术』Exp4:恶意代码分析 实验目标与基础问题 ++1.实践目标++ 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分析 ...

  8. web窗体的运用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebAp ...

  9. Dynamics CRM Online Administrator password reset

    道道还挺多,好好看看 Dynamics CRM Online Administrator password reset

  10. 模拟赛 sutoringu

    sutoringu 题意: 询问有多少一个字符串内有多少个个子区间,满足可以分成k个相同的串. 分析: 首先可以枚举一个长度len,表示分成的k个长为len的串.然后从1开始,每len的长度分成一块, ...