Flatten Nested List Iterator
class NestedIterator:
def __init__(self, nestedList: [NestedInteger]):
self.arr = []
def rec(node, cache):
if not node:
return
elif type(node) == int:
cache.append(node)
elif type(node) == list:
for e in node:
rec(e, cache)
elif node.isInteger():
cache.append(node.getInteger())
else:
for e in node.getList():
rec(e, cache)
rec(nestedList, self.arr)
self.lx = 0
def next(self) -> int:
temp = self.arr[self.lx]
self.lx = self.lx + 1
return temp
def hasNext(self) -> bool:
return self.lx < len(self.arr)