Linked List Cycle
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
hashset = set()
while head:
if head in hashset:
return True
else:
hashset.add(head)
head = head.next
return False def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False二刷
Last updated