Skip to content
Open
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
13 changes: 8 additions & 5 deletions data_structures/linked_list/has_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ def has_loop(self) -> bool:
>>> root_node.has_loop
True
"""
try:
list(self)
return False
except ContainsLoopError:
return True
slow_node = self
fast_node = self
while fast_node and fast_node.next_node:
slow_node = slow_node.next_node
fast_node = fast_node.next_node.next_node
if slow_node is fast_node:
return True
return False


if __name__ == "__main__":
Expand Down
Loading