码迷,mamicode.com
首页 > 编程语言 > 详细

【leetcode】Linked List Cycle (python)

时间:2014-07-16 17:18:53      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:algorithm   面试题   python   leetcode   

题目分析见这里

class Solution:
    # @param head, a ListNode
    # @return a list node
    def detectCycle(self, head):
        if None == head or None == head.next:
            return None
        pfast = head
        pslow = head
        #找第一次相遇的点,若存在环路,则肯定会相遇
        while pfast and pfast.next:
            pfast = pfast.next.next
            pslow = pslow.next
            if pslow == pfast: break
        if pslow != pfast:
            return None
        #pfast从头开始,则下次相遇的点就是循环开始的点
        pfast = head
        while True:
            if pfast == pslow:
                return pfast
            pfast = pfast.next
            pslow = pslow.next
            
            

【leetcode】Linked List Cycle (python),布布扣,bubuko.com

【leetcode】Linked List Cycle (python)

标签:algorithm   面试题   python   leetcode   

原文地址:http://blog.csdn.net/shiquxinkong/article/details/37818713

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!