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 ...
随机推荐
- Java 获取日期间的日期 & 根据日期获取星期
场景:根据起止日期获取中间的日期: 根据日期获取当前日期的星期 根据日期日期获取日期 /** * 获取日期间日期 * @param start * @param end * @return */ pr ...
- Python内置函数清单
作者:Vamei 出处:http://www.cnblogs.com/vamei Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些 ...
- 从程序员小仙飞升上神,java技术开发要如何实现?
新霸哥是一个专业从事java开发的,近期,新霸哥发现很多的朋友在问,从程序员小仙飞升上神难吗?在此新霸哥将为你详细的介绍,下面新霸哥将从新手入门和老司机进阶多方面详细的为大家介绍一下. 说起java首 ...
- 21、Nginx 常见问题
1.多个server_name容易产生冲突,会按照如下顺序匹配 1.首先选择所有的字符串完全匹配的server_name.(完全匹配) 2.选择通配符在前面的server_name,如*.bgx.co ...
- 2019.9.20使用kali中的metasploi获取windows 的权限
1 kali 基于debin的数字取证系统,上面集成了很多渗透测试工具,其前身是bt5r3(bractrack) 其中Metasploit是一个综合利用工具,极大提高攻击者渗透效率,使用ruby开发的 ...
- 记一次部署PHP遇到的编码问题故障
php开发给我项目和数据库,我按正常部署流程部署,开始发现之梦的后台登陆不了,后发现是属主属组不对,代码直接解压后是root的,更改后,后台能登陆,但部分显示乱码.后将正常的数据库文件重新导入后,显示 ...
- Dubbo 04 服务化最佳实现流程
Dubbo 04 服务化最佳实践 分包 建议将服务接口.服务模型.服务异常等均放在 API 包中,因为服务模型和异常也是 API 的一部分,这样做也符合分包原则:重用发布等价原则(REP),共同重用原 ...
- python-1.Centos7安装Python3.6和Scrapy的方法
由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到 [root@iZm5efjrz9szlsq1a0ai3gZ ~]# python ...
- (转)Linux传输大文件(分割传输)
1.分拆为多个文件的命令: cat workspace_2018.tar.gz | split -b 1G - workspace_2018.tar.gz. 命令解释: workspace_2018. ...
- 织梦网站dedecms防止挂马的思路
DedeCms做为国内使用最为广泛使用人数最多的CMS之一,经常爆出漏洞,每个漏洞的爆出,影响都是一大片,轻则被人挂广告.弹框,重则服务器成为肉机,宝贵数据丢失.那么有什么办法可以提高DedeCms的 ...