问题
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