错误描述
方法解释:
遍历所有节点,将遍历过的节点的next设置为特殊节点X,
终止条件:如果遍历到某节点的next==X,则说明此节点遍历过了,但是又遍历到它了,ok,return
否则,直接return None
代码:
class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def detectCycle(self , head ): if head==None: return None # 如果空链表, tmp = head if tmp.next==tmp: return tmp #如果自己指向自己 nodes = ListNode('@') while(tmp != None): if (tmp.next == nodes)or(tmp.next==tmp):#如果指向特殊节点,或自身 return tmp tmp2 = tmp.next tmp.next = nodes tmp = tmp2 return None # write code here