mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Optimized O(n) to O(1) (#11669)
This commit is contained in:
parent
00e9d86224
commit
918fa8bb8a
|
@ -14,11 +14,11 @@ class Node:
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
node = self
|
node = self
|
||||||
visited = []
|
visited = set()
|
||||||
while node:
|
while node:
|
||||||
if node in visited:
|
if node in visited:
|
||||||
raise ContainsLoopError
|
raise ContainsLoopError
|
||||||
visited.append(node)
|
visited.add(node)
|
||||||
yield node.data
|
yield node.data
|
||||||
node = node.next_node
|
node = node.next_node
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user