美文网首页
142. Linked List Cycle II

142. Linked List Cycle II

作者: JERORO_ | 来源:发表于2018-06-26 21:01 被阅读0次

问题描述

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

相关文章

网友评论

      本文标题:142. Linked List Cycle II

      本文链接:https://www.haomeiwen.com/subject/zntvyftx.html