Implement a class iterator to flatten a nested list of lists of integers. Each list element is either an integer or a list. There can be many levels of nested lists in lists.
The class initializes with a nested list. It also has two methods:
next() returns an integer in the order of appearance.hasNext() returns True / False regarding if all integers have been retrieved or not.Write the Class implementation for three required methods.
ni, actual = NestedIterator([[1, 1], 2, [1, 1]]), []
while ni.hasNext():
actual.append(ni.next())
actual ➞ [1, 1, 2, 1, 1]
ni, actual = NestedIterator([1, [4, [6]]]), []
while ni.hasNext():
actual.append(ni.next())
actual ➞ [1, 4, 6]
ni, actual = NestedIterator([[[]], []]), []
while ni.hasNext():
actual.append(ni.next())
actual ➞ []
N/A