zoukankan      html  css  js  c++  java
  • Linked List Cycle II

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

    Note: Do not modify the linked list.

    Follow up:
    Can you solve it without using extra space?

    这题要求判断是否有环,并找出环的入口,是Linked List Cycle的一个follow-up。

    如何找环,这个思路已经很熟悉了,就是设置一个双指针slow和fast,slow一次走1步,fast一次走2步。如何二者可以相遇,则说明链表有环,否则如果fast到了尾结点,则说明链表无环。

    查找环的入口,其实是一个数学推导问题。我们假设有环链表无环一段的长度为a,环的长度为b,则当slow和fast相遇时,设slow走过s,则fast走过2s,所以2s-s=s=k*b(k>=1)。即fast已经比slow在环上先绕了k圈。此时一个思路是让slow继续走,直至使s=a+k'*b时,则slow就走到了环的入口。当前s=k*b,则继续走a步就可以了,如何走a步呢,一个办法是让fast从链表开头开始走,则slow到达环入口时,fast也到达了。代码如下:

    class Solution(object):
        def detectCycle(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head:
                return None
            fast = slow = head
            while(fast and fast.next):
                fast = fast.next.next
                slow = slow.next
                if fast == slow:
                    break
            if fast == None or fast.next == None:
                return None
            fast = head
            
            while(fast!=slow):
                fast = fast.next
                slow = slow.next
            return slow

    代码时间复杂度为O(n),空间复杂度为O(1)。

  • 相关阅读:
    Jmeter四种参数化方式
    微信公众号开发--服务器接入
    IIS调试程序
    vs连接GitHub
    vs2013 卸载
    Edge,IE浏览器 兼容模式设置
    XML非法字符的处理
    SQL Server Union联合查询
    SQL Server NULL的正确用法
    SQL Server Like 与 通配符
  • 原文地址:https://www.cnblogs.com/sherylwang/p/5542966.html
Copyright © 2011-2022 走看看