如何查找链接列表中的中间元素而不遍历整个列表? [英] How to Find a middle element in a link list without traversing the entire list?

查看:126
本文介绍了如何查找链接列表中的中间元素而不遍历整个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找链接列表中的中间元素,而不必遍历整个列表。 ? ...最大可以只使用2个指针...怎么办? ....还没有给出列表的长度。

How to find a middle element in a link list without traversing the entire list . ? ...and at the maximum you can use only 2 pointers...how to do ? .... and also the length of list is not given.

推荐答案

没有看到你怎么可以做到没有遍历整个列表,除非你知道长度。

I don't see how you could do it without traversing the entire list unless you know the length.

我猜想答案要一个指针一次遍历一个元素,而第二个指针移动2个元素一个时间。

当第二个指针到达结束时,第一个指针将在中间。

I'm guessing the answer wants one pointer to be traversing one element at a time, while the second pointer moves 2 elements at a time.
This way when the second pointer reaches the end, the first pointer will be at the middle.

这篇关于如何查找链接列表中的中间元素而不遍历整个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆