快慢指针 判断是否是循环链表
2021-07-03 本文已影响0人
ES_KYW
func main() {
n1:=&LoopNode{}
n2:=&LoopNode{}
n3:=&LoopNode{}
n4:=&LoopNode{}
n5:=&LoopNode{}
n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5
n5.next = n1
flag := checkLoopNode(n1)
println(flag)
}
type LoopNode struct{
val int
next *LoopNode
}
func checkLoopNode(head *LoopNode) bool{
if head == nil {
return false
}
fast := head.next
slow := head
for fast != nil {
if slow == fast{
return true
}
fast = fast.next
}
return false
}
输出:true