Skip to content

Instantly share code, notes, and snippets.

@mdpabel
Created May 16, 2022 12:29
Show Gist options
  • Select an option

  • Save mdpabel/a2830aecd460c2319bff849950b00a1b to your computer and use it in GitHub Desktop.

Select an option

Save mdpabel/a2830aecd460c2319bff849950b00a1b to your computer and use it in GitHub Desktop.
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
seen = set()
node = head
while node:
if node in seen:
return True
else:
seen.add(node)
node = node.next
return False
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if not(head and head.next):
return False
slow , fast = head , head.next
while fast and fast.next:
if slow == fast:
return True
slow = slow.next
fast = fast.next.next
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment