1 class PeekingIterator:
 2     def __init__(self, iterator):
 3         self.iterator = iterator
 4         self.head = iterator.next()
 5 
 6     def peek(self):
 7         return self.head
 8 
 9     def next(self):
10         res = self.head
11         self.head = self.iterator.next() if self.iterator.hasNext() else None
12         return res
13 
14     def hasNext(self):
15         return self.head != None

题目类型:设计题,实现迭代器相关功能。

posted on 2020-04-06 08:16  Sempron2800+  阅读(140)  评论(0编辑  收藏  举报