LeetCode 284. Peeking Iterator--实现Iterator接口的hashNext和next方法,来实现peek方法

该博客介绍了如何设计并实现一个PeekingIterator,它在标准Iterator的基础上增加了peek()方法,允许查看下一次next()将返回的元素而不会消耗迭代。在构造函数中初始化时,next指向第一个非null节点。next()方法返回当前值并移动指针,而hashNext()只检查当前值是否为空,不移动指针。讨论了如何将设计扩展到支持所有类型的数据,而不只是整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next().

Here is an example. Assume that the iterator is initialized to the beginning of the list: [1, 2, 3].

Call next() gets you 1, the first element in the list.

Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.

You call next() the final time and it returns 3, the last element. Calling&nb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十六画生的博客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值