Flatten Nested List Iterator LeetCode solution in Python
1class NestedIterator:2def __init__(self, nestedList: [NestedInteger]):3self.flattened_lst = self.flattenList(nestedList)4self.idx = 056def next(self) -> int:7if self.idx >= len(self.flattened_lst):8raise Exception("Index out of bound")9self.idx += 110return self.flattened_lst[self.idx - 1]1112def hasNext(self) -> bool:13return self.idx < len(self.flattened_lst)1415def flattenList(self, lst):16flattened_lst = []17for ele in lst:18if ele.isInteger():19flattened_lst.append(ele.getInteger())20else:21flattened_lst.extend(self.flattenList(ele.getList()))22return flattened_lst