Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions data_structures/linked_list/has_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ def has_loop(self) -> bool:
>>> root_node.has_loop
True
"""
try:
list(self)
return False
except ContainsLoopError:
return True
slow = self
fast = self

while fast is not None and fast.next_node is not None:
slow = slow.next_node
fast = fast.next_node.next_node

if slow is fast:
return True

return False
if __name__ == "__main__":
root_node = Node(1)
root_node.next_node = Node(2)
Expand Down
Loading