Skip to content

Commit f69d387

Browse files
committed
NestedIterator: adjust javadoc, adjust solution & tests a bit; add YT link
1 parent 427d1d6 commit f69d387

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/main/java/by/andd3dfx/iterators/NestedIterator.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* or other lists. Implement an iterator to flatten it.
1515
*
1616
* Implement the NestedIterator class:
17-
* NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList.
17+
* NestedIterator(List&lt;NestedInteger&gt; nestedList) Initializes the iterator with the nested list nestedList.
1818
* int next() Returns the next integer in the nested list.
1919
* boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.
2020
*
@@ -29,15 +29,17 @@
2929
* If res matches the expected flattened list, then your code will be judged as correct.
3030
*
3131
* Example 1:
32-
* Input: nestedList = [[1,1],2,[1,1]]
33-
* Output: [1,1,2,1,1]
34-
* Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
32+
* Input: nestedList = [[1,1],2,[1,1]]
33+
* Output: [1,1,2,1,1]
34+
* Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
3535
*
3636
* Example 2:
37-
* Input: nestedList = [1,[4,[6]]]
38-
* Output: [1,4,6]
39-
* Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
37+
* Input: nestedList = [1,[4,[6]]]
38+
* Output: [1,4,6]
39+
* Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
4040
* </pre>
41+
*
42+
* @see <a href="https://youtu.be/RiJ7Rc98vkI">Video solution</a>
4143
*/
4244
public class NestedIterator implements Iterator<Integer> {
4345

@@ -76,11 +78,11 @@ private Integer determineNextElement() {
7678
while (!stack.isEmpty()) {
7779
Iterator<INestedInteger> iterator = stack.peek();
7880
if (iterator.hasNext()) {
79-
var next = iterator.next();
81+
INestedInteger next = iterator.next();
8082

81-
if (next.isInteger()) {
83+
if (next.isInteger()) { // is integer
8284
return next.getInteger();
83-
} else {
85+
} else { // is list
8486
stack.push(next.getList().iterator());
8587
return determineNextElement();
8688
}
@@ -91,6 +93,9 @@ private Integer determineNextElement() {
9193
return null;
9294
}
9395

96+
/**
97+
* This is the interface that allows for creating nested lists. You should not implement it, or speculate about its implementation
98+
*/
9499
public interface INestedInteger {
95100

96101
// @return true if this NestedInteger holds a single integer, rather than a nested list.

src/test/java/by/andd3dfx/iterators/NestedIteratorTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class NestedIteratorTest {
1313

1414
@Test
1515
public void testNextAndHasNext() {
16+
check(List.of(new NestedInteger(2)), List.of(2));
17+
check(List.of(new NestedIntegerList(new NestedIntegerList())), List.of());
18+
1619
check(List.of(
1720
new NestedIntegerList(1, 1),
1821
new NestedInteger(2),
@@ -72,7 +75,7 @@ public Integer getInteger() {
7275

7376
@Override
7477
public List<INestedInteger> getList() {
75-
throw new IllegalStateException();
78+
throw new IllegalStateException("Could not call getList on Integer!");
7679
}
7780
}
7881

@@ -102,7 +105,7 @@ public boolean isInteger() {
102105

103106
@Override
104107
public Integer getInteger() {
105-
throw new IllegalStateException();
108+
throw new IllegalStateException("Could not call getInteger on list!");
106109
}
107110

108111
@Override

0 commit comments

Comments
 (0)