23 lines
308 B
Go
23 lines
308 B
Go
|
|
package main
|
||
|
|
|
||
|
|
type ListNode struct {
|
||
|
|
Val int
|
||
|
|
Next *ListNode
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
|
||
|
|
}
|
||
|
|
func detectCycle(head *ListNode) *ListNode {
|
||
|
|
hash := make(map[*ListNode]struct{})
|
||
|
|
for head != nil {
|
||
|
|
if _, ok := hash[head.Next]; ok {
|
||
|
|
return head.Next
|
||
|
|
}
|
||
|
|
hash[head] = struct{}{}
|
||
|
|
head = head.Next
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|