For the interview, is it better to use the constructor to construct the entire flattened list first, or no?


  • 8
    I

    I noticed simple approach is to just flatten entire list first in constructor, since this makes next and hasNext O(1), even though constructor is O(N). Would an interviewer be ok with this approach, or would he want to actually dynamically maintain some kind of stack so that it always has the next integer?


  • 1
    G

    I think it depends on what the interviewer wants. These two approaches both work.


  • 1

    Definitely no. An iterator should never copy the input data structure.
    see my post for a comparison between different approaches.


  • 0
    H

    I agree..
    DO NOT flatten the list and store it .. that is not the point of the question...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.