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
快慢指针 判断是否是循环链表
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 题目一:判断单链表中是否有环 描述:1.有环的定义:链表的尾结点指向了链表中的某个结点 两种解决方案 【方法一】 ...