Leetcode Problem 284. Peeking Iterator

284. Peeking Iterator

Leetcode Solutions

Approach: Saving Peeked Value

  1. In the constructor, initialize a variable to store the iterator and a variable to store the next element.
  2. Initialize a boolean flag to indicate whether we have a peeked value.
  3. Implement the peek method to check if we have a peeked value. If not, retrieve the next element from the iterator and set the flag.
  4. Implement the next method to return the peeked value if available, or retrieve the next element from the iterator. Update the peeked value and flag accordingly.
  5. Implement the hasNext method to return true if we have a peeked value or if the iterator has a next element.
UML Thumbnail

Approach: Using a Queue to Store Peeked Values

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...