快慢指针 判断是否是循环链表
作者:
ES_KYW | 来源:发表于
2021-07-03 12:44 被阅读0次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
本文标题:快慢指针 判断是否是循环链表
本文链接:https://www.haomeiwen.com/subject/advuultx.html
网友评论