Skip to content

Instantly share code, notes, and snippets.

@thehydroimpulse
Created March 27, 2024 20:29
Show Gist options
  • Save thehydroimpulse/46ead8c8708eea81538e91e5f2ae15a2 to your computer and use it in GitHub Desktop.
Save thehydroimpulse/46ead8c8708eea81538e91e5f2ae15a2 to your computer and use it in GitHub Desktop.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# https://leetcode.com/problems/linked-list-cycle/description/
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if head is None:
return False
visited = {}
while head.next:
if head in visited:
return True
visited[head] = True
head = head.next
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment