feat: saturday
This commit is contained in:
28
pkg/linked_list_cycle/main.go
Normal file
28
pkg/linked_list_cycle/main.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package linked_list_cycle
|
||||||
|
|
||||||
|
type ListNode struct {
|
||||||
|
Val int
|
||||||
|
Next *ListNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func succeed(n *ListNode) *ListNode {
|
||||||
|
if n == nil {
|
||||||
|
return n
|
||||||
|
} else {
|
||||||
|
return n.Next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HasCycle(head *ListNode) bool {
|
||||||
|
one, two := head, succeed(head)
|
||||||
|
|
||||||
|
for one != nil && two != nil {
|
||||||
|
if one == two {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
one, two = succeed(one), succeed(succeed(two))
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user