package main
// 141 Linked List Cycle
func hasCycle(head *ListNode) bool {
// 快慢指针方法
//fast, slow := head, head
//for slow != nil && fast != nil {
// slow = slow.Next
// fast = fast.Next.Next
// if slow == fast{
// return true
// }
//}
//return false
// 哈希集合
isExistsMap := map[*ListNode]struct{}{}
for head != nil {
if _, ok := isExistsMap[head]; ok {
return true
}
isExistsMap[head] = struct{}{}
head = head.Next
}
return false
}
LeetCode 141. 环形链表
最新推荐文章于 2024-08-17 15:37:21 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)