142. Linked List Cycle II

2018-06-26  本文已影响0人  JERORO_

问题描述

Given a linked list, return the node where the cycle begins. If there is no cycle, return null

思路

说明.png
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow , fast = head , head
        meet = None
        loop = False
        while fast and fast.next and not loop:
            slow,fast = slow.next,fast.next.next
            if slow is fast: 
                loop = True
        if loop:
            start = head
            while start != slow:
                start = start.next
                slow = slow.next
            return slow
        return None  

图片参考

https://www.cnblogs.com/hiddenfox/p/3408931.html

上一篇 下一篇

猜你喜欢

热点阅读