【leetcode❤python】21. Merge Two Sorted Lists
#-*- 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
最新文章
- kettle参数、变量详细讲解[转]
- java基础,继承类题目:编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E
- 淘宝druid数据库连接池
- CSS3特性 盒模型 动画
- 简单理解ECMAScript2015中的Promise
- css cursor url用法格式详解
- c语言时间库函数#include<;time.h>;
- excel去除空格
- angular 按下回车键触发事件
- English trip EM1 - PE2 My My name is... Teacher:Lamb Key: introduce myself
- java 删除整数元素集合中的元素
- 64位版本为什么叫amd64,而不是intel64
- 14Junit、反射、注解
- Kernel 3.0.8 内存管理函数【转】
- 关于微信分享的一些心得之recommend.js(直接复制就行)
- VS2017调试闪退之Chrome
- R: Coercing LHS to a list
- QS Network---zoj1586最小生成树
- 人活着系列之开会(Floy)
- POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!
热门文章
- js 去除空格
- System.Web.HttpContext.Current.Server.MapPath(";~/upload/SH";) 未将对象引用设置为实例对象
- mongo快速翻页方法(转载)
- [OrangePi] Booting from USB drive
- 关于科台斯k97gprs调试记录(1)
- 【NOIP模拟赛】lover——心上人
- 锋利的JQuery(二)
- drupal 做301跳转(删除url里的www), 关键代码 可用到任何网站
- java中==与equal()方法的区别
- Web前端工作2个月小结