1 /**2 *//This is the interface, allows for creating nested lists.3 *//should not implement it, or speculate on its implementation4 * Public interface Nestedinteger {5 *6 * // @returntrue if this nestedinteger holds a single integer, rather than a nested list.7 * Public boolean isinteger ();8 *9 * // @returnThe single integer Nestedinteger holds, if it holds a single integerTen *//Return NULL if this nestedinteger holds a nested list One * Public Integer Getinteger (); A * - * // @returnthe nested list that this nestedinteger holds, if it holds a nested list - *//Return NULL if this nestedinteger holds a single integer the * Public list<nestedinteger> getList (); - * } - */ - Public classNestediteratorImplementsIterator<integer> { + PrivateDeque<integer>Flatten; - PublicNestediterator (list<nestedinteger>nestedlist) { +Flatten =NewLinkedlist<>(); A Getflatten (nestedlist); at } - - Private voidGetflatten (list<nestedinteger>nestedlist) { - for(inti = 0; I < nestedlist.size (); i++) { - if(Nestedlist.get (i). Isinteger ()) { - Flatten.offer (Nestedlist.get (i). Getinteger ()); in}Else { - Getflatten (Nestedlist.get (i). GetList ()); to } + } - } the * @Override $ PublicInteger Next () {Panax Notoginseng returnFlatten.pollfirst (); - } the + @Override A Public BooleanHasnext () { the returnFlatten.size () > 0; + } - } $ $ /** - * Your Nestediterator object would be instantiated and called as such: - * Nestediterator i = new Nestediterator (nestedlist); the * while (I.hasnext ()) v[f ()] = I.next (); - */
Flatten Nested List Iterator