You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 341. Flatten Nested List Iterator/341. Flatten Nested List Iterator.py
+13-11Lines changed: 13 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,13 @@
16
16
Explanation: By calling next repeatedly until hasNext returns false,
17
17
the order of elements returned by next should be: [1,4,6].
18
18
"""
19
+
20
+
19
21
# """
20
22
# This is the interface that allows for creating nested lists.
21
23
# You should not implement it, or speculate about its implementation
22
24
# """
23
-
#class NestedInteger:
25
+
#class NestedInteger:
24
26
# def isInteger(self) -> bool:
25
27
# """
26
28
# @return True if this NestedInteger holds a single integer, rather than a nested list.
@@ -40,20 +42,20 @@
40
42
41
43
classNestedIterator:
42
44
def__init__(self, nestedList: [NestedInteger]):
43
-
self.stack=nestedList[::-1]
44
-
45
-
45
+
# Copy the list into stack
46
+
47
+
self.stack=nestedList[:]
48
+
46
49
defnext(self) ->int:
47
-
returnself.stack.pop().getInteger()
48
-
50
+
returnself.stack.pop(0)
51
+
49
52
defhasNext(self) ->bool:
53
+
# We will check if there are values in the stack. If yes then we will first check if the first value is integer. Will add the values of a list into the stack otherwise.
0 commit comments