package main import "fmt" type ListNode struct{ val int next *ListNode } func hasCycle(head *ListNode) bool{ if head == nil || head.next == nil{ return false } var slow, fast = head, head.next for fast != slow{ if fast == nil || fast.next == nil{ return false } slow = slow.next fast = fast.next.next } return true } func main(){ a := new(ListNode) a.val = 1 b := new(ListNode) b.val = 2 c := new(ListNode) c.val = 3 a.next = b b.next = c fmt.Println(hasCycle(a)) }