【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
【leetcode❤python】21. Merge Two Sorted Lists的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- 【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 ...
- 【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 ...
- LeetCode记录之21——Merge Two Sorted Lists
算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...
- 【leetcode❤python】 88. Merge Sorted Array
#-*- coding: UTF-8 -*-class Solution(object): def merge(self, nums1, m, nums2, n): "& ...
- 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 ...
随机推荐
- PTPX的average power analysis
在average power analysis中,switching activity被分解为toggle rate和static probabilities两部分. annotation的sourc ...
- 三层与MVC
三层架构(3-tier architecture) 我们平时总是将三层架构与MVC混为一谈,殊不知它俩并不是一个概念.下面我来为大家揭晓我所知道的一些真相. 首先,它俩根本不是一个概念. 三层架构是一 ...
- windows中的上帝模式开启方法
在任何地方创建一个新的文件夹 把文件夹命名为"GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}" 回车,ok了
- update表关联
第一种: update student set student.age =(select `user`.age from user where id=student.id ) where studen ...
- SQL关于apply的两种形式cross apply和outer apply(转载)
SQL 关于apply的两种形式cross apply 和 outer apply apply有两种形式: cross apply 和 outer apply 先看看语法: <lef ...
- 使用uiautomatorviewer和uiautomator来做android的UI测试
来自:http://university.utest.com 作者:Angelos Nakulas (All Authored Courses) 译者:Elaine00 目录 简介 什 ...
- JS 字符串转日期格式 日期格式化字符串
/** * @author 陈维斌 http://www.cnblogs.com/Orange-C/p/4042242.html%20 3 * 如果想将日期字符串格式化,需先将其转换为日期类型Date ...
- jsp struts标签迭代各种数据
首先创建一个User对象 User user=new User(); user.setUserName("张三"); user.setAge(30); User user1=new ...
- android 项目学习随笔七(ViewPagerIndicator与ViewPager)
1.ViewPagerIndicator https://github.com/JakeWharton/ViewPagerIndicator package com.viewpagerindicato ...
- yum安装mysql后没有mysqld
在Centos中用命令 yum install mysql安装数据库,但装完后运行mysqld启动mysql的时候提示找不到,通过 find / | grep mysqld 也没找到mysqld的目录 ...