题目

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. java 通过接口在后台管理器中生成数据

    需求:测试人员在后台批量添加数据很麻烦,特别是针对一款商品配置了英语,还需要手动添加法语.俄语.阿拉伯语,很麻烦,但是因为没有项目组配合,做个小工具批量生成数据就只有自己去研究了 第一步:通过抓包工具 ...

  2. Nginx启用Gzip压缩js无效的原因

    Nginx启用gzip很简单,只需要设置一下配置文件即可完成,可以参考文章Nginx如何配置Gzip压缩功能.不过,在群里常有人提到,他们的网站Gzip压缩虽然成功了,但检测到JS仍然没有压缩成功,这 ...

  3. 2017.11.29 JSP+Servlet 中功能验证码及验证的实现

    源代码如下: validate.jsp <%@ page language="java" import="java.util.*" pageEncodin ...

  4. Spring Security 之集群Session配置

    1.   新建Maven项目 cluster-session 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4.0. ...

  5. 大数据(1)初始hadoop

    1.hadoop模型如下: (上图为Hadoop1.x的布局) (Hadoop2.x较Hadoop1.x,多了YARN) Hadoop框架,是一个庞大的生态系统. 或者我们可以这样理解: 可以把整个体 ...

  6. CTS、CLS、CLR分别作何解释?

    CTS.CLS.CLR分别作何解释? 答:CTS:通用类型系统.CLS:通用语言规范.CLR:公共语言运行库.

  7. face verification

    一直没怎么思考过人脸应用上cnn是怎么回事,也曾经想当然过.这两天看些文章,才明白思路. 在人脸verification上,cnn起到的是一个特征提取作用,通过cnn提取得到features,基于这些 ...

  8. ImportError : cannot import name main

    当我们有时候安装不成功插件或者其他模块时候,会有pip报错hu@hu-VirtualBox:~/下载/MySQL-python-1.2.4b4$ pip install pymysqlTracebac ...

  9. JSPatch库, 一个Apple官方支持的实现在线更新iOS应用的库

    简介 项目主页: https://github.com/bang590/JSPatch 示例下载: https://github.com/ios122/ios122 JSPatch 可以让你用 Jav ...

  10. 第35题:LeetCode138. Copy List with Random Pointer

    题目 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 考点 思路 代码 /** * Definition for singly ...