# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: nodePointers = set() while head is not None: if head in nodePointers: return True else: nodePointers.add(head) head = head.next return False