返回信息流我的解答在leetcode上会超时。
想请教python大神。这道题怎么解才能不超时?
这是一条镜像帖。来源:北邮人论坛 / python / #16829同步于 2016/12/2
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
142. Linked List Cycle II python解答如何不超时?
yohu
2016/12/2镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
【 在 yohu 的大作中提到: 】
: 我的解答在leetcode上会超时。
: 想请教python大神。这道题怎么解才能不超时?
据说在赋初值的时候不要令 fast = head.next.next 就能解决这个问题
嗯嗯,我想过。可是我只是用的网上的解答方法。
不是自己的思路,所以不好意思贴上来。刚刷题,能力还不行。
【 在 Daci 的大作中提到: 】
: 你应该贴上你的代码呀。只是想看答案可以看discuss。
那不要fast = head.next.next; 应该怎么改呢?
【 在 solosseason 的大作中提到: 】
: 据说在赋初值的时候不要令 fast = head.next.next 就能解决这个问题
贴出来无妨哈
【 在 yohu (yohu) 的大作中提到: 】
: 嗯嗯,我想过。可是我只是用的网上的解答方法。
: 不是自己的思路,所以不好意思贴上来。刚刷题,能力还不行。
【 在 yohu 的大作中提到: 】
: 那不要fast = head.next.next; 应该怎么改呢?
:
```python
slow = fast = head
hascycle = false
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
if slow == fast:
hascycle = True
break
if hascycle:
...
else:return None
```
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head != None and head.next != None:
s,f = head,head.next
else:
return None
while s != f:
if f == None or f.next == None:
return None
s,f = s.next,f.next.next
if s != head:
s,f = s.next,head
while s != f:
s,f = s.next,f.next
return s