Flatten Nested List Iterator, is a LeetCode problem. In this post we will see how we can solve this challenge in Swift

## Problem Description

You can find the full details of the problem Flatten Nested List Iterator at LeetCode

## Solution

Every time pop a element and push back until it is an integer.

Time Complexity I: next - O(1), hasNext - O(n), Space Complexity I: O(n)

Primary idea II: Flatten the nested list at very first.

Time Complexity II: next - O(1), hasNext - O(1), Space Complexity II: O(n)

// This is the interface that allows for creating nested lists.

// You should not implement it, or speculate about its implementation

class NestedInteger {

``````// Return true if this NestedInteger holds a single integer, rather than a nested list.

public func isInteger() -> Bool

// Return the single integer that this NestedInteger holds, if it holds a single integer

// The result is undefined if this NestedInteger holds a nested list

public func getInteger() -> Int

// Set this NestedInteger to hold a single integer.

public func setInteger(value: Int)

// Set this NestedInteger to hold a nested list and adds a nested integer to it.

// Return the nested list that this NestedInteger holds, if it holds a nested list

// The result is undefined if this NestedInteger holds a single integer

public func getList() -> [NestedInteger]``````

}

Please check the main.swift snippet for the solution.

This solution originally posted at: Github by @soapyigu