- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
java.util.LinkedList 中 addLast()、add() 或 Offer() 等效方法的渐近复杂度是多少?是 O(N) 还是 O(1)?也就是说,LinkedList 内部是保留一个指向其尾部的指针,还是从头部开始遍历链表?
无论哪种方式,您将如何利用 FIFO 队列的具体实现,该实现在 Offer() 方法中效率更高,但仍使用标准库? (没有自定义队列实现)。 LinkedList 是一个好的选择还是其他选择?
我意识到这个问题之前可能有人问过,但我搜索了很长时间后找不到答案。
最佳答案
是的,列表保留了一个指向尾部的指针,正如您可以在 class JavaDoc 中读到的那样。 .
All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
因此像 addLast()
和 add()
这样的操作需要 O(1) 时间。
文档特别指出 LinkedList 适合队列实现。
Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue.
关于java.util.LinkedList addLast() 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25207602/
我正在学习 netty,示例代码如下 ChannelPipeline pipeline = pipeline(); // Enable stream compression (you can remo
我目前正在致力于实现一个用于发送数据包的队列。但是,我遇到了一个问题,当我在 LinkedList 上使用 addLast 函数时,它会将列表中的每个 Pair 替换为我添加到其中的 Pair。 队列
我必须在列表末尾添加一个元素,所以现在我使用 LinkedList 的 addLast() 函数在末尾添加元素。它确实在列表中添加元素当且仅当之后没有更多的add()语句。 下面工作正常。 Linke
我正在学习 netty,示例代码如下 ChannelPipeline pipeline = pipeline(); // Enable stream compression (you can remo
如何在双向循环链表的末尾插入一个项目?为什么我运行时会出现 NullPointerException ? public void addLast( String title, double le
我创建了一个 DeQueue 如下 Deque deque = new ArrayDeque(8); // use add() method to add elements in the deque
我正在用 C 语言创建链表数据结构。但是,我在执行 addLast 函数时收到一些奇怪的行为。直到我下次调用 addLast 时,添加的元素似乎才出现。我的代码(我将通过内联注释解释我认为我的代码是如
java.util.LinkedList 中 addLast()、add() 或 Offer() 等效方法的渐近复杂度是多少?是 O(N) 还是 O(1)?也就是说,LinkedList 内部是保留一
我正在努力将一个项目添加到链接列表的末尾(这不是家庭作业......只是我自己的练习)。 这是程序: public class CustomLinkedList { private stati
我在声明 ArrayList 后使用 addLast 方法时遇到问题。这是类代码: import java.util.*; public class Neuron{ public in
我有一个类,它应该创建一个带有值链接列表的哈希表。如果 key 是新的,它会创建一个链接列表,如果它已经存在,则应该将其附加到列表的末尾。由于某种原因,当我使用 addLast() 时,它会替换列表的
import java.util.List; import java.util.LinkedList; class Test { public static void main(String[
这个问题在这里已经有了答案: Linked list head double pointer passing (5 个答案) 关闭 8 年前。 我希望有人能解释为什么我的教授在他给我们的这个示例代码
我有一个奇怪的问题,我真的无法理解。 我用这种方式创建了一个LinkedList: List customList = new LinkedList(); 如果使用 list instanceof L
我的代码: 注意:readInt() 和 readString() 等函数是普林斯顿大学 algs4.jar 包的一部分。 import java.util.Iterator; import java
在我的 Java 应用程序中,以下两个都将编译和运行,并产生所需的结果。 //"Rotate" the list items one place to the left. myLinkedList.a
我有一个 LinkedList在我的项目中,我添加和删除了很多元素(每秒几百个)。这发生在通过锁同步的多个线程中。 现在有时(可能每隔 100.000 个元素)我得到一个 NullReferenceE
ArrayDeque docs声明push(E)与 addFirst(E) 相同,和pop()与 removeFirst() 相同. 我认为使用 addLast(E) 可以达到相同的效果+ remov
我是一名优秀的程序员,十分优秀!