简体   繁体   English

从arraylist和linkedlist中删除最后一个元素时的时间复杂度

[英]Time Complexity while deleting last element from arraylist and linkedlist

1st Part :- 第一部分: -

I was reading in the book - "Data Structure and Algorithms made easy in Java" that the time complexity for deleting the last element from Linkedlist and Arraylist is O(n). 我在书中读到 - “Java中的数据结构和算法变得简单”,从Linkedlist和Arraylist中删除最后一个元素的时间复杂度是O(n)。 But the Linkedlist internally implements DoublyLinkedlist so the time complexity should be O(1) and similarly for Arraylist as it internally implement Array it should be O(1). 但是Linkedlist内部实现了DoublyLinkedlist,因此时间复杂度应为O(1),对于Arraylist来说类似,因为它在内部实现Array,它应该是O(1)。

2nd Part :- 第二部分: -

It also says that the insertion of an element at the ending of a linkedlist has a time complexity of O(n) but the linkedlist maintains pointers both at the end and at the front. 它还说在链表的末尾插入一个元素的时间复杂度为O(n),但是链表在末尾和前面都保持指针。 So it this statement correct ? 那么这句话是否正确? Moreover it says that the time complexity to insert an element in an arraylist at the end is O(1) if array is not full and O(n) if the array is full. 此外,它表示如果数组未满,则在结尾处插入元素的时间复杂度为O(1),如果数组已满,则为O(n)。 Why O(n) if the array is full ? 为什么O(n)数组是否已满?

Thanks for answering the 1st part . 感谢您回答第1部分。 Can anyone please also explain the 2nd part. 任何人都可以请解释第二部分。 Thanks :) 谢谢 :)

It depends on what methods you're calling. 这取决于你所采用的方法。

A glance at the implementation shows that if you're calling LinkedList.removeLast() , that's O(1). 一瞥实现就会发现,如果你正在调用LinkedList.removeLast() ,那就是O(1)。 The LinkedList maintains pointers both to the first and last node in the list. LinkedList维护指向列表中第一个和最后一个节点的指针。 So it doesn't have to traverse the list to get to the last node. 因此,它不必遍历列表以到达最后一个节点。

Calling LinkedList.remove(index) with the index of the last element is also O(1), because it traverses the list from the closest end. 使用最后一个元素的索引调用LinkedList.remove(index)也是O(1),因为它遍历最近端的列表。 [Noted by user @andreas in comment below.] [用户@andreas在下面的评论中注明。]

But if you're calling LinkedList.remove(Object) , then there's an O(n) search for the first matching node. 但是如果你正在调用LinkedList.remove(Object) ,那么就会对第一个匹配节点进行O(n)搜索。

Similarly, for ArrayList, if you're calling ArrayList.remove(index) with the index of the last element, then that's O(1). 类似地,对于ArrayList,如果您使用最后一个元素的索引调用ArrayList.remove(index) ,那么那就是O(1)。 For all other indices, there's a System.arrayCopy() call that can be O(n) -- but that's skipped entirely for the last element. 对于所有其他索引,有一个System.arrayCopy()调用可以是O(n) - 但是完全跳过了最后一个元素。

But if you call ArrayList.remove(Object) , then again there's an O(n) search for the first matching node. 但是如果你调用ArrayList.remove(Object) ,那么再次对第一个匹配节点进行O(n)搜索。

Removing last element of a linked list will take O(n) if it has to traverse all the records to reach the end. 如果必须遍历所有记录以到达结尾,则删除链表的最后一个元素将采用O(n)。 But implementation of java.util.LinkedList which is doubly-linked , does not keep reference only to head, but also keeps a reference of tail and allows you to remove last in O(1). 但是双重链接的java.util.LinkedList的实现不仅仅保留对head的引用,而且还保留tail的引用并允许你在O(1)中删除last。 It is implemented in a clever way to traverse the list from the beginning or the end, whichever is closer to the specified index 它以一种巧妙的方式实现,从开头或结尾遍历列表,以较接近指定索引为准

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM