快慢指针
func hasCycle(head *ListNode) bool {
if head==nil||head.Next==nil {
return false
}
for head!=nil{
if head.Val==1>>63 {
return true
}
head.Val = 1>>63
head = head.Next
}
return false
}
map
func hasCycle2(head *ListNode) bool {
if head==nil||head.Next==nil {
return false
}
m :=make(map[uintptr]struct{})
for head!=nil{
if _,ok:=m[uintptr(unsafe.Pointer(head))];ok {
return true
}
m[uintptr(unsafe.Pointer(head))] = struct{}{}
head = head.Next
}
return false
}