Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GitMsw opened this issue Apr 21, 2020 · 1 comment

Comments

@GitMsw
Copy link

GitMsw commented Apr 21, 2020

应该是: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
Copy link
Author

GitMsw commented Apr 21, 2020

上面的我搞错了,正确应该是这样吧。。
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant