linked_list_cycle
linked_list_cycle.py 源码
# 判断链表是否有环
# https://leetcode-cn.com/problems/linked-list-cycle
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 使用一个set
def hasCycle_1(self, head: ListNode) -> bool:
s = set()
while head:
if head in s:
return True
s.add(head)
head = head.next
return False
# 快慢指针
def hasCycle_2(self, head: ListNode) -> bool:
slow, fast = head, head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
if slow == fast:
return True
return False
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦