2024-03-06 16:05:23 +0300 MSK
Linked List Cycle
Links
Code
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func hasCycle(head *ListNode) bool {
nodes := map[*ListNode]struct{}{}
for head != nil {
if _, ok := nodes[head]; ok {
return true
}
nodes[head] = struct{}{}
head = head.Next
}
return false
}