链表的归并排序

超时的代码

class Solution:
def merge(self, head1, head2):
if head1 == None:
return head2
if head2 == None:
return head1
# head1 and head2 point to the same link list
if head1 == head2:
return head1 head = None
tail = None
# the small pointer point to smaller of two.
while head1 and head2:
if head1.val <= head2.val:
small = head1
head1 = head1.next
else:
small = head2
head2 = head2.next
# the first node
if tail == None:
tail = small
head = small
else:
tail.next = small
tail = small
# link the remaind nodes
if head1 == None:
head1 = head2 tail.next = head1
return head def 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 list
slow = head
fast = head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
# slow point to the middle now
head2 = slow.next
# we cut of the linked list at middle
slow.next = None left = 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 head2
if head2 == None:
return head1
# head1 and head2 point to the same link list
if head1 == head2:
return head1 head = ListNode(-1)
tail = head
# the small pointer point to smaller of two.
while head1 and head2:
if head1.val <= head2.val:
small = head1
head1 = head1.next
else:
small = head2
head2 = head2.next tail.next = small
tail = small
# link the remaind nodes
if head1 == None:
head1 = head2 tail.next = head1
return head.next def 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 list
slow = head
fast = head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
# slow point to the middle now
head2 = slow.next
# we cut of the linked list at middle
slow.next = None left = self.sortList(head)
right = self.sortList(head2)
return self.merge(left, right)

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

【leetcode】sort list(python)的更多相关文章

  1. 【leetcode】Word Break(python)

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

  2. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  3. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. 【LeetCode】Reconstruct Itinerary(332)

    1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...

  5. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  6. 【leetcode】3Sum Closest(middle)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  8. 【链表】Sort List(归并排序)

    题目: Sort a linked list in O(n log n) time using constant space complexity. 思路: nlogn的排序有快速排序.归并排序.堆排 ...

  9. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

随机推荐

  1. 产看Linux运行时间

    Linux下如何查看系统启动时间和运行时间 1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.0 ...

  2. (三)使用XML配置SQL映射器

    SqlSessionFactoryUtil.java package com.javaxk.util; import java.io.IOException; import java.io.Input ...

  3. 使用celery时要注意的任务调用形式

    因为之前,一直用django和celery紧密集成,不分家. 所以使用时参考了网上的配置之后,没有变更过. 最近,和洪军想用k8s的pod重新规划系统构架时,这个问题才又浮了出来. 只是我们的task ...

  4. day5模块学习--hashlib模块

    hashlib模块     Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度 ...

  5. Linux文件系统备份dump

    常用的备份方式有三种:1.完全备份:把所有数据完全备份下来2.增量备份:以上一次备份的内容作参照3.差异备份:一直以某一个记录点的全备份作参照备份 dump备份工具dump命令:dump -数字 数字 ...

  6. 关于 eclipse启动卡死的问题 解决方法

    关于 eclipse启动卡死的问题(eclipse上一次没有正确关闭,导致启动的时候卡死错误解决方法),自己常用的解决方法: 方案一(推荐使用,如果没有这个文件,就使用方案二): 到<works ...

  7. NHibernate 学习导航

    http://www.cnblogs.com/lyj/archive/2008/10/30/1323099.html

  8. tomcat下程序包的安装与部署

    还没亲自在服务器进行部署,但是参考了公司文档,等自己安装部署的时候,再将文档补充完整. 1.初始化数据库 2.修改war包的数据 主要包括的一些配置是数据库的连接配置. 3.将包发布 这个步骤下有几个 ...

  9. MongoDB入门教程三[数据类型]

    MongoDB的文档使用BSON(Binary JSON)来组织数据,BSON类似于JSON,JSON只是一种简单的表示数据的方式,只包含了6种数据类型(null.布尔.数字.字符串.数组及对象),不 ...

  10. c++ 栈(顺序表)

    栈可以用顺序表(数组)也可以用链表来储存内容,本文采用顺序表(数组)来保存内部元素.代码如下:  1 #include <iostream> 2 using namespace std; ...