MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / has_loop

Method has_loop

data_structures/linked_list/has_loop.py:26–43  ·  view source on GitHub ↗

A loop is when the exact same Node appears more than once in a linked list. >>> root_node = Node(1) >>> root_node.next_node = Node(2) >>> root_node.next_node.next_node = Node(3) >>> root_node.next_node.next_node.next_node = Node(4) >>> root_node.has_l

(self)

Source from the content-addressed store, hash-verified

24
25 @property
26 def has_loop(self) -> bool:
27 """
28 A loop is when the exact same Node appears more than once in a linked list.
29 >>> root_node = Node(1)
30 >>> root_node.next_node = Node(2)
31 >>> root_node.next_node.next_node = Node(3)
32 >>> root_node.next_node.next_node.next_node = Node(4)
33 >>> root_node.has_loop
34 False
35 >>> root_node.next_node.next_node.next_node = root_node.next_node
36 >>> root_node.has_loop
37 True
38 """
39 try:
40 list(self)
41 return False
42 except ContainsLoopError:
43 return True
44
45
46if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected