链表的归并排序

超时的代码

class Solution:def merge(self, head1, head2):if head1 == None:return head2if head2 == None:return head1# head1 and head2 point to the same link listif head1 == head2:return head1head = Nonetail = None# the small pointer point to smaller of two.while head1 and head2:if head1.val <= head2.val:small = head1head1 = head1.nextelse:small = head2head2 = head2.next# the first nodeif tail == None:tail = smallhead = smallelse:tail.next = smalltail = small# link the remaind nodesif head1 == None:head1 = head2tail.next = head1return headdef sortList(self, head):if head == None or head.next == None:return head# we use a fast pointer which go two steps each time and # a slow pointer which go one step each time to get the # middle of the link listslow = headfast = headwhile fast.next and fast.next.next:fast = fast.next.nextslow = slow.next# slow point to the middle nowhead2 = slow.next# we cut of the linked list at middleslow.next = Noneleft = self.sortList(head)right = self.sortList(head2)return self.merge(left, right)

主要是在merge的时候。要推断第一个结点

AC代码

class Solution:def merge(self, head1, head2):if head1 == None:return head2if head2 == None:return head1# head1 and head2 point to the same link listif head1 == head2:return head1head = ListNode(-1)tail = head# the small pointer point to smaller of two.while head1 and head2:if head1.val <= head2.val:small = head1head1 = head1.nextelse:small = head2head2 = head2.nexttail.next = smalltail = small# link the remaind nodesif head1 == None:head1 = head2tail.next = head1return head.nextdef sortList(self, head):if head == None or head.next == None:return head# we use a fast pointer which go two steps each time and # a slow pointer which go one step each time to get the # middle of the link listslow = headfast = headwhile fast.next and fast.next.next:fast = fast.next.nextslow = slow.next# slow point to the middle nowhead2 = slow.next# we cut of the linked list at middleslow.next = Noneleft = self.sortList(head)right = self.sortList(head2)return self.merge(left, right)

这里merge的时候建立了一个伪头结点,处理的时候就不用推断是否为第一个结点,尽管AC,但时间5500ms以上。而c++代码仅仅须要200多ms,差距还是比較大的

转载于:https://www.cnblogs.com/bhlsheji/p/5384318.html

【leetcode】sort list(python)相关推荐

  1. 【leetcode】Word Break(python)

    思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...

  2. 算法之【动态规划】详解(python)

    算法之动态规划详解 定义 动态规划其实是一种运筹学方法,是在多轮决策过程中寻找最优解的方法. 应用场景 动态规划问题的一般形式就是求最值.动态规划其实是运筹学的一种最优化方法,只不过在计算机问题上应用 ...

  3. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  5. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  6. 【机器学习】概率神经网络(PNN)的python实现

    [机器学习]概率神经网络(PNN)的python实现 一.概率神经网络原理 1.1.贝叶斯决策 1.2.PNN的网络结构 二.概率神经网路的优点与不足 2.1.优点(参考资料[1]) 2.2.缺点 三 ...

  7. 【LeetCode】动态规划入门(专项打卡21天合集)

    [LeetCode]动态规划入门(专项打卡21天合集) 下图为证 文章目录 [LeetCode]动态规划入门(专项打卡21天合集) Day1 斐波拉契数 第 N 个泰波那契数 Day2 爬楼梯 使用最 ...

  8. 【机器学习】基于奇异值分解(SVD)的协同过滤推荐算法及python实现

    [机器学习]基于奇异值分解(SVD)的协同过滤推荐算法及python实现 一.协同过滤推荐算法 1.1.协同过滤算法的分类 1.2.相似度的度量 1.3.商品评分的预测 二.奇异值分解(SVD)在协同 ...

  9. 【JDK7】新特性(2) 语法

    2019独角兽企业重金招聘Python工程师标准>>> JDK7对Java语法有少量更新,重点是在易用性和便捷性的改进.     1.二进制字面量 JDK7开始,终于可以用二进制来表 ...

最新文章

  1. python格式化html库_用Python格式化HTML代码
  2. 古剑奇谭网络版服务器位置,《古剑奇谭网络版》服务器数据互通调研公告
  3. python爬虫代码1000行-最精简的爬虫 --仅需4行代码(python)
  4. Android系统编译so库提示error undefined reference to '__android_log_print问题的解决
  5. Excel中配置VBA的工作环境
  6. 2021暑假实习-SSM超市积分管理系统-day05笔记
  7. sql 2008找不到服务器,sql server 2005 数据库迁移问题总结——错误 ‘80004005’ 在 sys.servers 中找不到服务器 ‘XXX’...
  8. 盗版Windows系统IE 7.0的安装
  9. python百分号字符串_python--003--百分号字符串拼接、format
  10. js基础知识汇总08
  11. java 静态方法 非静态变量_深度分析:Java 静态方法/变量,非静态方法/变量的区别,今天一并帮你解决!...
  12. 数字图像处理第三次试验:图像复原、图像分割
  13. 微信小程序实现垂直tab标签页的切换及动态的选中下划线移动-纵向
  14. 统一认证 java_java统一身份认证系统
  15. 万物互联开发平台NSDK
  16. 使用位运算求正数的相反数
  17. 模板类继承后找不到父类函数的问题
  18. “互联网+”拯救了星巴克,出路是“第四空间
  19. scala特质 对比java的接口 使用方法
  20. B-样条基函数:定义 (B-spline Basis Functions: Definition)

热门文章

  1. Mysql Linux安装详细步骤
  2. 分位数回归的R语言实现
  3. 聚类分析应用之市场细分
  4. linux 如何选择新的内核,如何更新内核??__boot_内核_vmlinuz_linux_System__169IT.COM
  5. java点歌系统代码_ktv 一个用java写的ktv点歌系统,用ACCESS数据库 Develop 238万源代码下载- www.pudn.com...
  6. tomcat websock html5,websocket实战(4) websocket版贪食蛇游戏(tomcat官方自带)
  7. 系统学习机器学习之参数方法(二)
  8. 通过高速计算机网络和多媒体,全国2014.10办公自动化原理及应用试题
  9. 分区表的误区:性能提升
  10. maven 阿里云仓库