版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412854
思路
快慢指针,如果相遇则有环
code
代码语言:javascript复制func hasCycle(head *ListNode) bool {
if head == nil {
return false
}
fast := head.Next
slow := head
for slow != nil && fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
if fast == slow {
return true
}
}
return false
}