leet-code/linked-list-cycle/sol.go

27 lines
399 B
Go
Raw Normal View History

2022-06-04 11:25:07 +00:00
package main
type ListNode struct {
Val int
Next *ListNode
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func hasCycle(head *ListNode) bool {
exists := map[*ListNode]bool{}
current := head
for current != nil {
exists[current] = true
current = current.Next
if exists[current] {
return true
}
}
return false
}