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
# Below is the interface for Iterator, which is already defined for you.## class Iterator(object):# def __init__(self, nums):# """# Initializes an iterator object to the beginning of a list.# :type nums: List[int]# """## def hasNext(self):# """# Returns true if the iteration has more elements.# :rtype: bool# """## def next(self):# """# Returns the next element in the iteration.# :rtype: int# """classPeekingIterator:
def__init__(self, iterator):
self.iter=iteratorifself.iter.hasNext():
self.cur_value=self.iter.next()
else:
self.cur_value=Nonedefpeek(self):
returnself.cur_valuedefnext(self):
ans=self.cur_valueifself.iter.hasNext():
self.cur_value=self.iter.next()
else:
self.cur_value=NonereturnansdefhasNext(self):
returnself.cur_valueisnotNone# Your PeekingIterator object will be instantiated and called as such:# iter = PeekingIterator(Iterator(nums))# while iter.hasNext():# val = iter.peek() # Get the next element but not advance the iterator.# iter.next() # Should return the same value as [val].