#-*- 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

最新文章

  1. kettle参数、变量详细讲解[转]
  2. java基础,继承类题目:编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E
  3. 淘宝druid数据库连接池
  4. CSS3特性 盒模型 动画
  5. 简单理解ECMAScript2015中的Promise
  6. css cursor url用法格式详解
  7. c语言时间库函数#include&lt;time.h&gt;
  8. excel去除空格
  9. angular 按下回车键触发事件
  10. English trip EM1 - PE2 My My name is... Teacher:Lamb Key: introduce myself
  11. java 删除整数元素集合中的元素
  12. 64位版本为什么叫amd64,而不是intel64
  13. 14Junit、反射、注解
  14. Kernel 3.0.8 内存管理函数【转】
  15. 关于微信分享的一些心得之recommend.js(直接复制就行)
  16. VS2017调试闪退之Chrome
  17. R: Coercing LHS to a list
  18. QS Network---zoj1586最小生成树
  19. 人活着系列之开会(Floy)
  20. POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!

热门文章

  1. js 去除空格
  2. System.Web.HttpContext.Current.Server.MapPath(&quot;~/upload/SH&quot;) 未将对象引用设置为实例对象
  3. mongo快速翻页方法(转载)
  4. [OrangePi] Booting from USB drive
  5. 关于科台斯k97gprs调试记录(1)
  6. 【NOIP模拟赛】lover——心上人
  7. 锋利的JQuery(二)
  8. drupal 做301跳转(删除url里的www), 关键代码 可用到任何网站
  9. java中==与equal()方法的区别
  10. Web前端工作2个月小结