题目

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. 如何使用Win32API绘制树

    一.这个已经有几年时间了,刚开始学习charlie的<windows程序设计>的时候做的.现在看来,代码很乱,虽然后来还整理过几次,现在这方面没什么兴趣了,有兴趣的可自由下载. 二.绘制二 ...

  2. Android(java)学习笔记62:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解

    1. 先看看网路上的说法: android.intent.action.MAIN 决定应用程序最先启动的 Activity android.intent.category.LAUNCHER 决定应用程 ...

  3. LA 2957 最大流,最短时间,输出路径

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=1 ...

  4. 调用外部EXE文件

    实现效果: 知识运用: Process类的Start方法 实现代码: private void button1_Click(object sender, EventArgs e) { OpenFile ...

  5. Entity Framework的扩展库

    https://github.com/jcachat/EntityFramework.DynamicFilters Provides global & scoped filters for E ...

  6. C/C++语言代码规范

    1.标识符名称: 标识符名称包括函数名.常量名.变量名等.这些名字应该能反映它所代表的实际东西,具有一定的意义,使其能 够见名知义,有助于对程序功能的理解.规则如下: 所有宏定义.枚举常数和const ...

  7. CUDA 纹理内存

    原文链接 1.概述 纹理存储器中的数据以一维.二维或者三维数组的形式存储在显存中,可以通过缓存加速访问,并且可以声明大小比常数存储器要大的多. 在kernel中访问纹理存储器的操作称为纹理拾取(tex ...

  8. vs2012或vs2013调试卡 关闭调试卡

    以前vs2013就有这个问题.没有解决.今天又装了vs2012.又遇到了.特别郁闷. 今天一定要解决.网上百度了.很久.可能关键字有问题.没有找到好的办法. 找到的办法有.显卡问题.不是管理员运行问题 ...

  9. JVM性能调优(out of memory内存溢出/泄露出来)

    JVM基础知识: JVM调优工具: 1.jmap jmap常用参数 命令:jmap -heap PID >> D:\heap.log 解释: using thread-local obje ...

  10. Layer弹出层关闭后刷新父页面

    API地址:http://layer.layui.com/api.html#end 调用END回调方法: end - 层销毁后触发的回调 类型:Function,默认:null 无论是确认还是取消,只 ...