# CODE

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

class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:

while(fast != None and slow != None):
fast = fast.next.next
slow = slow.next

if fast == slow:
return True

return False
``````
• ListNode的理解

``````# Definition for singly-linked list.
import time

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
# 有环状的情况
if m == pos:
cir = listN
print(listN.val)
print('---------------')

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

fast = tmp
slow = tmp

while(fast != None and slow !=None):
if(fast.next == None):
return False
fast = fast.next.next
slow = slow.next

if(fast == slow):
return True

S = Solution()

print("___________________________________________________________________________________")

# 中间变量，用来记录处理头listNode节点
tmp = listN
print(listN)
print(tmp)
print(listN.val)
print("#############################")
pos = 1
print("m\t" + str(m))
listN = listN.next
# 有环状的情况
# 标记成环的地方
if m == pos:
cir = listN
print(listN.val)
print('---------------')

# 成环
try:
if cir.next != None:
listN.next = cir
print('try')
except Exception as ex:
print("Exception"+ex)

print(tmp)
print(listN)
print("#############################")
# while True:
#     print(listN.val)
#     if listN.next != None:
#         listN = listN.next
#         continue
#     else:
#         print(listN.next)

# 不能直接用ListN
# 因为上面一直在移动
# 建立一个中间的tmp来表示初始list
while tmp.next != None:
print(tmp.val)
print(tmp)
tmp = tmp.next
time.sleep(1)
``````

### 要不赞赏一下?

 微信 支付宝 PayPal Bitcoin

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

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