本文介绍了Java中的LinkedList上调用size()的时间复杂度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如标题所示,我想知道LinkedList类中的size()方法是否需要分摊O(1)时间或O(n)时间。
As the title asks, I wonder if the size() method in the LinkedList class takes amortized O(1) time or O(n) time.
推荐答案
这是O(1)。你可以谷歌搜索源代码,你会得到这样的:
It's O(1). You can google for the source code and you will come to such:
来自
我看过的所有Collection类都将大小存储为变量,并且不会遍历所有内容来获取它。
All of the Collection classes I have looked at store a the size as a variable and don't iterate through everything to get it.
这篇关于Java中的LinkedList上调用size()的时间复杂度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!