Solution

• Accepted
``````# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
def detectCycle(self, head: ListNode) -> ListNode:

while(fast != None and slow !=None):
# 无环的情况
if(fast.next == None):
return None

fast = fast.next.next
slow = slow.next

if(fast == slow):
while(fast != slow):
slow = slow.next
fast = fast.next
return slow
break
``````
• Time Limit Exceeded，有点疑惑为啥单独搞一个ListNode就不行呢？
``````class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:

# 环状链表的构造
tmp = listN
cir = listN
# pos 为-1时则无环
pos = 1
listN = listN.next
# 有环状的情况
# 这中忽略了pos为0的情况
if m == pos:
cir = listN

if cir.next != None:
listN.next = cir

# solve this problem

get_pos = ListNode(-1)

fast = tmp
slow = tmp

while(fast != None and slow !=None):
# 无环的情况
if(fast.next == None):
get_pos.val = None
return get_pos

fast = fast.next.next
slow = slow.next

if(fast == slow):
slow = tmp
while(fast != slow):
slow = slow.next
fast = fast.next
get_pos.val += 1
return get_pos
break

S = Solution()
``````

要不赞赏一下?

 微信 支付宝 PayPal Bitcoin

``https://www.emperinter.info/2022/02/21/linked-list-cycle-ii/``

 阿里云国际版 20美元 Vultr 10美元 搬瓦工 | Bandwagon 应该有折扣吧？ Just My Socks JMS9272283 【注意手动复制去跳转】 域名 | namesilo `emperinter`(1美元) 币安 币安