【问题标题】:What is the time complexity of LinkedList.getLast() in Java?Java 中 LinkedList.getLast() 的时间复杂度是多少?
【发布时间】:2011-02-15 11:35:33
【问题描述】:

我在 Java 类中有一个私有 LinkedList,并且经常需要检索列表中的最后一个元素。列表需要扩展,因此我试图决定是否需要在进行更改时保留对最后一个元素的引用(以实现 O(1)),或者 LinkedList 类是否已经通过 getLast() 调用完成了该操作.

LinkedList.getLast() 的大 O 成本是多少?是否记录在案?(即我可以依赖这个答案还是不应该做任何假设和缓存它,即使它是 O(1)?)

【问题讨论】:

    标签: java performance collections big-o


    【解决方案1】:

    这是 O(1),因为列表是双向链接的。它保留对头部和尾部的引用。

    来自文档:

    索引到列表中的操作将从开头或结尾遍历列表,以更接近指定索引的为准。

    【讨论】:

    • 它不仅是双向的,而且是循环的。
    • helpermethod 的“循环”注释清楚地回答了这个问题。
    • @helpermethod,谢谢。今天我学习了 LinkedList 是如何组织的。早些时候我认为它保持对最后一个元素的引用。
    【解决方案2】:

    它是 O(1),你不应该缓存它。 getLast 方法只返回header.previous.element,因此没有计算,也没有遍历列表。当您需要在其中查找元素时,链表会变慢,因为它从一端开始,一次移动一个元素。

    【讨论】:

      【解决方案3】:

      来自LinkedList 文档:

      所有操作都按照双向链表的预期执行。索引到列表中的操作将从开头或结尾遍历列表,以更接近指定索引的为准。

      它应该是 O(1),因为双向链表将引用它自己的尾部。 (即使它没有明确保留对其尾部的引用,找到它的尾部也是 O(1)。)

      【讨论】:

        【解决方案4】:

        LinkedList.getLast() 的实现毫无疑问 - 这是一个 O(1) 操作。 但是,我没有发现它在任何地方都有记录。

        【讨论】:

        • 您可能需要像 Yaneeve 那样查看 Java 源代码以了解实现细节。您可以将 java 核心库源代码附加到 IDE。
        【解决方案5】:

        来自 Java 6 源代码:

        * @author  Josh Bloch
         * @version 1.67, 04/21/06
         * @see     List
         * @see     ArrayList
         * @see     Vector
         * @since 1.2
         * @param <E> the type of elements held in this collection
         */
        
        public class LinkedList<E>
            extends AbstractSequentialList<E>
            implements List<E>, Deque<E>, Cloneable, java.io.Serializable
        {
            private transient Entry<E> header = new Entry<E>(null, null, null);
            private transient int size = 0;
        
            /**
             * Constructs an empty list.
             */
            public LinkedList() {
                header.next = header.previous = header;
            }
        
        ...
        
            /**
             * Returns the first element in this list.
             *
             * @return the first element in this list
             * @throws NoSuchElementException if this list is empty
             */
            public E getFirst() {
            if (size==0)
                throw new NoSuchElementException();
        
            return header.next.element;
            }
        
            /**
             * Returns the last element in this list.
             *
             * @return the last element in this list
             * @throws NoSuchElementException if this list is empty
             */
            public E getLast()  {
            if (size==0)
                throw new NoSuchElementException();
        
            return header.previous.element;
            }
        
        ...
        
        }
        

        所以 getFirst()getLast() 都是 O(1)

        【讨论】:

          猜你喜欢
          • 2012-02-13
          • 2013-05-21
          • 1970-01-01
          • 1970-01-01
          • 2014-10-04
          • 1970-01-01
          • 2018-11-24
          • 2014-05-27
          相关资源
          最近更新 更多