Created
March 27, 2024 20:29
-
-
Save thehydroimpulse/46ead8c8708eea81538e91e5f2ae15a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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