#-*- coding: UTF-8 -*-
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#Method1
class Solution(object):
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1==None:return l2
        if l2==None:return l1
        
        node=None
        
        while l1!=None and l2!=None:

            if l1.val<=l2.val:
                if node==None:
                    node=l1
                    head=node
                else:
                    node.next=l1
                    node=node.next
                l1=l1.next
            else:
              
                if node==None:
                    node=l2
                    head=node
                else:
                    node.next=l2
                    node=node.next
                l2=l2.next
        
        node.next=l1 or l2
        return head
    
    
#Method2
class Solution:   
    def mergeTwoLists(self, l1, l2):  
        if not l1 and not l2:  
            return None  
 
        dummy = ListNode(0)  
        cur = dummy  
        while l1 and l2:  
            if l1.val <= l2.val:  
                cur.next = l1  
                l1 = l1.next  
            else:  
                cur.next = l2  
                l2 = l2.next  
            cur = cur.next  
        cur.next = l1 or l2  
 
        return dummy.next

【leetcode❤python】21. Merge Two Sorted Lists的更多相关文章

  1. [LeetCode&Python] Problem 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 ...

  2. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  3. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  4. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

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

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

  7. LeetCode记录之21——Merge Two Sorted Lists

    算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...

  8. 【leetcode❤python】 88. Merge Sorted Array

    #-*- coding: UTF-8 -*-class Solution(object):    def merge(self, nums1, m, nums2, n):        "& ...

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

随机推荐

  1. RMAN备份演练初级篇

    前面我们已经知道了如何进入rman,以及rman的一些基本命令,相信大家定会觉着rman操作的简单,事实也确实如此,但万不要因此小视rman的强大,简单往往意味着灵活,灵活对于那些有心人则意味着主动权 ...

  2. R语言书籍的学习路线图

    现在对R感兴趣的人越来越多,很多人都想快速的掌握R语言,然而,由于目前大部分高校都没有开设R语言课程,这就导致很多人不知道如何着手学习R语言. 对于初学R语言的人,最常见的方式是:遇到不会的地方,就跑 ...

  3. [Ubuntu] 转载-使用Ubuntu修复grub

    原文地址:http://fav.coketea.com/article_show.php?id=1 步骤一.以试用方式进入ubuntu光盘系统,打开终端(快捷键ctrl+alt+t): 步骤二.获取r ...

  4. [OrangePi] Installation on internal EMMC

    Install the image on SD Card as described above Boot your Orange PI board from SD Card Run: sudo ins ...

  5. nginx在windwos中的使用

    本文章参考了 nginx for windows的介绍:http://nginx.org/en/docs/windows.html 你从官网上下载到的是一个 zip 格式的压缩包,首先要把压缩包解压. ...

  6. ReportingService 通过RowNumber函数获取行号和生成隔行变色样式

    以前一直没有搞明白SSRS里面的RowNumber函数到底该怎么用,所以一直没有很好的办法在SSRS中的表格上实现隔行变色的样式,实现隔行变色的关键就是获取表格中每一行的行号.在最近了解了下这个函数, ...

  7. 关于UIView(转)

    曾经有人这么说过,在iphone里你看到的,摸到的,都是UIView,所以UIView在iphone开发里具有非常重要的作用.那么UIView我们到底知道多少呢.请看看下面的问题, 如果这些你都知道, ...

  8. scala构建类似java的pojo

    主要看以下代码: package com.test.scalaw.test.demo import scala.beans.BeanProperty /** * scala构建类似java 的pojo ...

  9. Install Sogou IM 2.0 in Ubuntu14.04+/Xfce

    Ubuntu14.04+ 安装搜狗输入法 搜狗输入法是一款非常友好的输入法产品,从Ubuntu14.04开始对Linux支持,不过只是Debian系的,是Ubuntu优麒麟组引入的.优麒麟是针对国人设 ...

  10. JavaEE基础(二十四)/多线程

    1.多线程(多线程的引入) 1.什么是线程 线程是程序执行的一条路径, 一个进程中可以包含多条线程 多线程并发执行可以提高程序的效率, 可以同时完成多项工作 2.多线程的应用场景 红蜘蛛同时共享屏幕给 ...