Cycle Detection

  • + 0 comments
    def has_cycle(head):
        visited = set()
        while n:=head.next:
            if n in visited:
                return 1
            else:
                visited.add(n)
                head = n
        return 0