Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created April 13, 2021 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/8e2e40ca9eaeac4dc1ea2971c9fce777 to your computer and use it in GitHub Desktop.
Save vrat28/8e2e40ca9eaeac4dc1ea2971c9fce777 to your computer and use it in GitHub Desktop.
Flatten Nested List Iterator Solution (Swift)
class NestedIterator {
private var list:[Int]
private var currentIndex: Int
init(_ nestedList: [NestedInteger]) {
self.list = []
currentIndex = 0
flatten(nestedList)
}
func flatten(_ nestedList: [NestedInteger]) {
for element in nestedList {
print(element)
if element.isInteger() {
list.append(element.getInteger())
}
else{
flatten(element.getList())
}
}
}
func next() -> Int {
defer {
currentIndex += 1
}
return list[currentIndex]
}
func hasNext() -> Bool {
return currentIndex < list.count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment