package main type ListNode struct { Val int Next *ListNode } func main() { } func hasCycle(head *ListNode) bool { hash := make(map[*ListNode]struct{}) for head != nil { if _, ok := hash[head.Next]; ok { return true } hash[head] = struct{}{} head = head.Next } return false }