#-*- 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. PAT乙级 1004. 成绩排名 (20)

    1004. 成绩排名 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入n名学生的姓名.学号.成绩,分 ...

  2. ThinkPHP讲解(八)——显示、修改、添加、删除

    一.显示数据 <h1>主页面</h1> <table width="100%" border="1" cellpadding=&q ...

  3. 【crunch bang】安装firefox,删除iceweasel

    首先,移除iceweasel: apt-get remove iceweasel Then, download the latest Linux build of Firefox directly f ...

  4. cornerstone的简单使用

    第一步打开工具点击左下角那个加号,弹出的选项中选Add Repository..或者直接点中间显示的Add Repository...是一样的 第二步: 下面填的内容是你经理给你的账号和密码,你只要c ...

  5. grads 读取浓度值

  6. php 的 构造函数 和 析构函数

    构造函数 在C++ java里的应用及其普遍,今天好好研究了一下 php 的 构造函数 和 析构函数 构造函数 和 析构函数 构造函数 void __construct ([ mixed $args ...

  7. 28、Oracle(四)用户权限控制

    一)用户Oracle中的用户分为二大类1)Oracle数据库服务器创建时,由系统自动创建的用户,叫系统用户,如sys.2)利用系统用户创建的用户,叫普通用户,如scott,hr,c##tiger,zh ...

  8. ectouch第二讲之 文件结构

    相信大家在ectouch官网都注意到了,ectouch采用的MVC框架,之前一直以为它用的和ecshop一样都是smarty,本鸟默默按照smarty的文件结构研究了好几天,结果是各种文件对不上号.无 ...

  9. Android中的图片压缩

    1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB ...

  10. [STL]set/multiset用法详解[自从VS2010开始,set的iterator类型自动就是const的引用类型]

    集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...