leetcode-easy-listnode-21 merge two sorted lists
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的更多相关文章
- 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&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
算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...
- 【leetcode❤python】21. Merge Two Sorted Lists
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 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 ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- 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 ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- 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 ...
随机推荐
- javascript--获取一个页面各个标签的数量
获取一个页面各个标签的数量 document.getElementsByTagName('*')--获取所有的标签. var obj = document.getElementsByTagName(' ...
- openlayers之点,线,面(以城市,河流,省份为例,分别对应点线面)
kpst._this这里指向初始化的map // 设置标注样式函数 function createStyle(name) { // 河流style var riverStyle = new Style ...
- RaspberryPi交叉编译环境配置-Ubuntu & wiringPi & Qt
1.配置RaspberryPi交叉编译环境: 在开发RaspberryPi Zero的过程中,由于Zero板卡的CPU的处理性能比较弱,因此其编译的性能比较弱,需要将代码在PC电脑上交叉编译完成之后再 ...
- 加上这几个组件,flask摇身一变是django
写在前面 flask和django作为python中的两大开源框架,各分春秋,各有各自的优点,不能一概而论说哪个好哪个不好.flask框架小而精,适用于快速开发一些小的应用的项目.django大而全, ...
- sqlalchemy 中的get_or_404
- CVE漏洞分析
分析cve-2018-9489漏洞和download content provider(CVE-2018-9468, CVE-2018-9493, CVE-2018-9546), 每人至少选择一个漏洞 ...
- 如何在某些情况下禁止提交Select下拉框中的默认值或者第一个值(默认选中的就是第一个值啦……)
群里有个帅哥问了这么个问题,他的下拉框刚进页面时是隐藏起来的,但是是有值的,为啥呢?因为下拉框默认选中了第一个值呗,,, 所以提交数据的时候就尴尬啦,明明没有选,但是还是有值滴.怎么办呢? 一开始看到 ...
- python基础(变量、基础数据类型、流程控制)
今日内容html {overflow-x: initial !important;}:root { --bg-color:#ffffff; --text-color:#333333; --select ...
- jquery $(".classc",$(".classp"))的含义
jquery中有一种选择器的写法为:$(".classc",$(".classp")),注意,是$()中又嵌套了一个$(),这种写法的作用类似于$(" ...
- 初识linux(简单命令)
之前一直搞不懂,为什么全是命令行的linux系统这么多公司都在用,当你看不懂那一行行命令时你一定会和我一样觉得头大.但当你学习了命令再结合桌面版觉得linux还是挺不错的