Interview-code-practice-python icon indicating copy to clipboard operation
Interview-code-practice-python copied to clipboard

判断一个链表是否有环,如果有的话返回第一个进环的节点.py

Open GitMsw opened this issue 6 years ago • 1 comments

应该是:if fast == None or slow == None: 才返回None吧? 而且是返回return fast.next这个才是第一个进环的结点,直接返回fast是前一个结点

我写了测试发现的,楼主看下 3 <main.Node object at 0x000001E414C26080> 4 <main.Node object at 0x000001E414C260B8> 5 <main.Node object at 0x000001E414C26048> 3 <main.Node object at 0x000001E414C26080> 4 <main.Node object at 0x000001E414C260B8> 5 <main.Node object at 0x000001E414C26048> 3 <main.Node object at 0x000001E414C26080> 4 <main.Node object at 0x000001E414C260B8>

结果= <main.Node object at 0x000001E414C26080>

GitMsw avatar Apr 21 '20 10:04 GitMsw

上面的我搞错了,正确应该是这样吧。。 def getloopmeet(head): fast = head.next.next slow = head.next while fast != slow: if fast == None or slow == None: return None fast = fast.next.next slow = slow.next fast = head while fast != slow: fast = fast.next slow = slow.next return fast

GitMsw avatar Apr 21 '20 10:04 GitMsw