class Node(object):
def __init__(self, value, next_node=None):
self.next_node = next_node
self.value = value
def is_circular(self):
head, visited = self, set()
while head is not None:
i = id(head)
if i in visited:
return True
visited.add(i)
head = head.next_node
return False
def find_cycle(lst):
for i, node in enumerate(lst):
if hasattr(node, 'position'):
return node.position
else:
node.position = i