mycode

一定要记得创建两个头哦,一个找路,一个找家

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
l = dummy = ListNode(-1)
while l1 or l2:
if not l1:
l.next = l2
break
elif not l2:
l.next = l1
break
if l1.val < l2.val:
l.next = l1
l1 = l1.next
else:
l.next = l2
l2 = l2.next
l = l.next
return dummy.next

参考

下面这个更快

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def mergeTwoLists(self, l1, l2): res = temp = ListNode(0) while l1 or l2:
v1 = v2 = float('inf')
if l1 is not None: v1 = l1.val
if l2 is not None: v2 = l2.val
if v1 > v2:
temp.next = l2
l2 = l2.next
else:
temp.next = l1
l1 = l1.next
temp = temp.next return res.next

leetcode-easy-listnode-21 merge two sorted lists的更多相关文章

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

  2. [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 ...

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

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

  4. 【leetcode❤python】21. Merge Two Sorted Lists

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

  5. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

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

  7. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  8. 21. Merge Two Sorted Lists(合并2个有序链表)

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  9. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  10. C# 写 LeetCode easy #21 Merge Two Sorted Lists

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

随机推荐

  1. weex animation模块 使用指南

    本节学习目标 掌握内置组件animation的使用 我们在开发应用的时候,常常需要增加一些动画效果,来提高用户体验,经常用到的一些动画效果如下 平移 旋转 缩放 背景颜色改变 组件透明图 weex 提 ...

  2. 第二十二篇 jQuery 学习4 内容和属性

    jQuery 内容和属性   这节课,我们学习使用jQuery来控制元素的内容.值和属性.   html() 控制所选元素的内容(包括HTML标记): text() 控制所选元素的内容: val() ...

  3. oracle 12C的新特性-CDB和PDB

    1.前言 CDB与PDB是Oracle 12C引入的新特性,在ORACLE 12C数据库引入的多租用户环境(Multitenant Environment)中,允许一个数据库容器(CDB)承载多个可插 ...

  4. jq无限极树结构

    //群组树结构$(function () { var params= { "companyId":cmpId }; var loadUrl="/apiv2/classif ...

  5. ssh无密码访问设置(ssh-keygen 的详解)

    [原文链接]http://blog.csdn.net/wh_19910525/article/details/7433164 为了让两个linux机器之间使用ssh不需要用户名和密码.所以采用了数字签 ...

  6. U-boot新手入门,烧写进mini2440

    拿到一块开发板,首先就要找到它的资料,当然了,开发板的厂商或者代理商会提供资料,资料里会有你需要的. 比如我的这块mini2440,在友善之臂代理商提供的资料里面,就有我们这篇所需要的 把这个文件夹下 ...

  7. 第06课:GDB 常用命令详解(下)

    本课的核心内容: disassemble 命令 set args 和 show args 命令 tbreak 命令 watch 命令 display 命令 6.1 disassemble 命令 当进行 ...

  8. volatile关键字解决线程间内存共享变量同步的问题,让变量可以立即同步。

  9. HDU4456-Crowd (坐标旋转处理+hash处理+二维树状数组)

    题意: 给出一个矩阵,初始每个位置上的值都为0,然后有两种操作 一种是更改某个位置上的值 另一种是求某个位置附近曼哈顿距离不大于K的所有位置的值的总和 技巧: 坐标旋转,使得操作之后菱形变成方方正正的 ...

  10. 使用IDEA搭建一个Spring + AOP (权限管理 ) + Spring MVC + Mybatis的Web项目 (零配置文件)

    前言: 除了mybatis 不是零配置,有些还是有xml的配置文件在里面的. 注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration  ...