My solution with Queue


  • 1
    public class NestedIterator
    {
        Queue<int> q;
        public NestedIterator(IList<NestedInteger> nestedList)
        {
            q = new Queue<int>();
            FillQ(nestedList);
        }
    
        private void FillQ(IList<NestedInteger> list)
        {
            foreach(NestedInteger ni in list)
            {
                if (ni.IsInteger())
                    q.Enqueue(ni.GetInteger());
                else
                    FillQ(ni.GetList());
            }
        }
    
        public bool HasNext()
        {
            return q.Any();
        }
    
        public int Next()
        {
            return q.Dequeue();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.