The real time efficiency of LinkedList in Java

∥☆過路亽.° 提交于 2019-12-12 18:21:58

问题


We know the Double LinkedList Data Structure has an advantage of inserting a node in O(1) time if you already got the node before or after the location you want to insert. (e.g. if you have a double linked list: A-B-C-D, if you already got the node C, then it only takes O(1) time to insert a new Node before or after the node C).

If you manually construct a double linked list in Java/C++, it is fairly easy to understand, but I recently am interested in the LinkedList library in Java which is a double linked list data structure offered in java.util. If I want to use the library LinkedList provided by java, how could I perform O(1) insertion or deletion like I mentioned in the 1st paragraph? I did some research, you can create a ListIterator of the LinkedList which can traverse forward and back then insert and delete the former or after node. But it still needs traverse. If I already have the node C and how could I directly get the corresponding Iterator in O(1) time?


回答1:


The LinkedList class provides O(1) insertion/deletion time during traversing or at the beginnig/end of the list. This doesn't mean that you can take a random node in the middle of the list and delete it or insert some node next to it in O(1) time.



来源:https://stackoverflow.com/questions/53528087/the-real-time-efficiency-of-linkedlist-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!