题目

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

代码:oj在线测试通过 Runtime: 208 ms

 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param two ListNodes
# @return a ListNode
def mergeTwoLists(self, l1, l2):
if l1 is None:
return l2
if l2 is None:
return l1 dummyhead = ListNode(0)
dummyhead.next = None
p = dummyhead
while l1 is not None and l2 is not None:
if l1.val > l2.val:
p.next = l2
l2 = l2.next
else:
p.next = l1
l1 = l1.next
p = p.next
if l1 is not None:
p.next = l1
else:
p.next = l2
return dummyhead.next

思路

虚表头dummyhead设定好了就不要动,设定一个p=hummyhead,然后从处理p.next开始;最后返回hummyhead就可以获得正确的结果

两个表的指针一步步移动,并通过比较val的大小决定哪个插入p.next

注意,每次执行完判断,要移动指针p=p.next

leetcode 【 Merge Two Sorted Lists 】 python 实现的更多相关文章

  1. leetcode Merge K sorted Lists python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  2. leetcode Merge Two Sorted Lists python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  3. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  4. [LeetCode] 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 ...

  5. LeetCode: Merge Two Sorted Lists 解题报告

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

  6. LeetCode: Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  7. [Leetcode] Merge k sorted lists 合并k个已排序的链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...

  8. 21. Merge Two Sorted Lists —— Python

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  9. LeetCode:Merge k Sorted Lists

    题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  10. LeetCode——Merge k Sorted Lists

    Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...

随机推荐

  1. thinkphp 去掉URL 里面的index.php

    例如你的原路径是 http://localhost/test/index.php/home/goods/index.html 那么现在的地址是 http://localhost/test/home/g ...

  2. How good are detection proposals, really?

    How good are detection proposals, really? J. Hosang, R. Benenson, B. Schiele Oral at BMVC 2014 http: ...

  3. java后台导出pdf

    新页面打开wpf @RequestMapping("/showPdf") public String getpic( HttpServletRequest request, Htt ...

  4. C#中datatable的用法/传数据

    在开发中,我们常用到表类型数据,不同于string,int....那么datatable类型如何定义呢,具体怎么使用呢,代码如下: namespace Common.Table { using Sys ...

  5. 在centos7云服务器上搭建Apache服务器并访问到你的网站

    使用X-shell ssh安全连接到云服务器 https://mail.qq.com/cgi-bin/mail_spam?action=check_link&url=https://www.n ...

  6. Linux - bashrc之alias

    1. cd ~ 2. touch .bashrc // 若该文件不存在的话 3. vim .bashrc ----------------复制粘贴如下文本--------------- # alias ...

  7. lvs+keepalived实验

    一.VRRP协议与工作原理 在现实网络环境中,主机之间的通信都是通过配置静态路由或者(默认网关)来完成的,而主机之间的路由器一旦发生故障,通信就会失效,因此这种通信模式当中,路由器就成了一个单点瓶颈, ...

  8. 使用 python快速搭建http服务

    在 Linux 服务器上或安装了 Python 的机器上,Python自带了一个WEB服务器 SimpleHTTPServer. 我们可以很简单的使用  python -m SimpleHTTPSer ...

  9. MAC中向阿里云服务器上传文件

    打开mac中的终端 使用命令:$scp /local/file user@remote:/file /local/file 是本地文件 后面部分[用户名]@[ip地址:][服务器中的文件目录] not ...

  10. Windows下安装Python数据库模块--MySQLdb

    ## 1.下载MySQLdb [去官网](http://pypi.python.org/pypi/MySQL-python/) 下载对应的编译好的版本(现在官网最新版本为1.2.5): MySQL-p ...