You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.

Implement the NestedIterator class:


	NestedIterator(List&lt;NestedInteger&gt; nestedList) Initializes the iterator with the nested list nestedList.
	int next() Returns the next integer in the nested list.
	boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.


Your code will be tested with the following pseudocode:

initialize iterator with nestedList
res = []
while iterator.hasNext()
    append iterator.next() to the end of res
return res


If res matches the expected flattened list, then your code will be judged as correct.

&nbsp;
Example 1:

Input: nestedList = [[1,1],2,[1,1]]
Output: [1,1,2,1,1]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].


Example 2:

Input: nestedList = [1,[4,[6]]]
Output: [1,4,6]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].


&nbsp;
Constraints:


	1 &lt;= nestedList.length &lt;= 500
	The values of the integers in the nested list is in the range [-106, 106].


class NestedIterator:
    def __init__(self, nestedList: [NestedInteger]):
        self.flattened_lst = self.flattenList(nestedList)
        self.idx = 0
    
    def next(self) -> int:
        if self.idx >= len(self.flattened_lst):
            raise Exception("Index out of bound")
        self.idx += 1
        return self.flattened_lst[self.idx-1]
    
    def hasNext(self) -> bool:
        return self.idx < len(self.flattened_lst)
    
    def flattenList(self, lst):
        flattened_lst = []
        for ele in lst:
            if ele.isInteger():
                flattened_lst.append(ele.getInteger())
            else:
                flattened_lst.extend(self.flattenList(ele.getList()))
        return flattened_lst
public class NestedIterator implements Iterator<Integer> {
    List<Integer> list=new ArrayList();
    void flatten(List<NestedInteger> nestedList){
        for(NestedInteger nested:nestedList){
            if(nested.isInteger())
                list.add(nested.getInteger());
            else
                flatten(nested.getList());
        }
    }
    public NestedIterator(List<NestedInteger> nestedList) {
        flatten(nestedList);
    }
    int index=0;
    @Override
    public Integer next() {
        return list.get(index++);
    }

    @Override
    public boolean hasNext() {
        return index<list.size();
    }
}
class NestedIterator {
    vector<int> v;
    int index;
    void helper(vector<NestedInteger> &nestedList, vector<int>& v)
    {
        for (int i = 0; i < nestedList.size(); i++)
        {
            if (nestedList[i].isInteger())
            {
                v.push_back(nestedList[i].getInteger());
            }
            else
            {
                helper(nestedList[i].getList(), v);
            }
        }
    }
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        helper(nestedList, v);
        index = 0;
    }
    
    int next() {
        return v[index++];
    }
    
    bool hasNext() {
        if (v.size() <= index)
        {
            return false;
        }
        return true;
    }
};
/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * function NestedInteger() {
 *
 *     Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     @return {boolean}
 *     this.isInteger = function() {
 *         ...
 *     };
 *
 *     Return the single integer that this NestedInteger holds, if it holds a single integer
 *     Return null if this NestedInteger holds a nested list
 *     @return {integer}
 *     this.getInteger = function() {
 *         ...
 *     };
 *
 *     Return the nested list that this NestedInteger holds, if it holds a nested list
 *     Return null if this NestedInteger holds a single integer
 *     @return {NestedInteger[]}
 *     this.getList = function() {
 *         ...
 *     };
 * };
 */
/**
 * @constructor
 * @param {NestedInteger[]} nestedList
 */
var NestedIterator = function(nestedList) {
    this.nestedList = []
    this.ptr = 0
    const dfs = (elem) => {
        if(!elem.isInteger()) {
            let list = elem.getList()
            for(let i = 0;i<list.length;i++) {
                dfs(list[i])
            }
        }else {
            this.nestedList.push(elem.getInteger())
        }
    }
    for(let i = 0;i<nestedList.length;i++) {
        dfs(nestedList[i])
    }
};


/**
 * @this NestedIterator
 * @returns {boolean}
 */
NestedIterator.prototype.hasNext = function() {
    if(this.ptr < this.nestedList.length) return true
    else return false
};

/**
 * @this NestedIterator
 * @returns {integer}
 */
NestedIterator.prototype.next = function() {
    return this.nestedList[this.ptr++]
};

/**
 * Your NestedIterator will be called like this:
 * var i = new NestedIterator(nestedList), a = [];
 * while (i.hasNext()) a.push(i.next());
*/
Flatten Nested List Iterator