链表的归并排序

超时的代码

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. [android]Intent跳转新的Activity可以传递数据过去

    两种方式: 一,直接通过Bundle对象来传递: 如果我们想要给“收件人”Activity说点什么的话,那么可以通过下面这封“E-mail”来将我们的消息传递出去 Intent intent=new ...

  2. c 语言文本文件判断是否到达结尾的问题

    在c语言中,判断文件结尾有两种方法,第一种是使用feof()函数,feof(fp)用于测试fp所指向的文件的当前状态是否为“文件结束”.如果是,函数则返回的是非0值(真),否则为0(假),要注意的是, ...

  3. Spring+Dubbo集成Redis的两种解决方案

    当下我们的系统数据库压力都非常大,解决数据库的瓶颈问题势在必行,为了解决数据库的压力等需求,我们常用的是各种缓存,比如redis,本文就来简单讲解一下如何集成redis缓存存储,附github源码. ...

  4. Spark(十三)SparkSQL的自定义函数UDF与开窗函数

    一 自定义函数UDF 在Spark中,也支持Hive中的自定义函数.自定义函数大致可以分为三种: UDF(User-Defined-Function),即最基本的自定义函数,类似to_char,to_ ...

  5. day6面向对象--类的特殊成员方法

    类的特殊成员方法 1. __doc__ 表示类的描述信息 class Func(object): '''__doc__方法是用来打印类的描述信息''' def tell(self): pass def ...

  6. js和php计算图片自适应宽高算法实现

    js Code: <script> $width = $(imgobj).width(); //图原始宽 $newheight = $(imgobj).height(); //图原始高 $ ...

  7. LOADRUNNER连接ORACLE数据库的方法

    LOADRUNNER连接ORACLE数据库的方法     最近正在做一个测试数据库性能的项目,直接写出来的连接数据库并且进行数据库查询和插入的脚本在VUSER_INIT中(连接数据库)#include ...

  8. xmanager

    [root@upright91 run]# ./runBenchmark.sh updbtpcc.properties sqlTableCreates Exception in thread &quo ...

  9. ubuntu12.04上的mongodb卸载

    如果您需要卸载 mongodb,然后有几种方法来完成这取决于你想实现. 一.卸载只是 mongodb 这将删除只是 mongodb 包本身. 1 sudo apt-get remove mongodb ...

  10. Scala入门4(_的用法)

    从网上找了一篇博客,详细讲解了Scala下划线的用法,这里做保留 博客链接