The whole idea of LinkedList is to improve such operations, while paying with reduced iteration performance.
No, the idea of a linked list is to make adding or removing at the end or beginning very cheap ... or if you already have a reference to a node object, adding around or deleting it is very cheap too. (I don't think the Java API provides nodes, but some other platforms, such as .NET, do.)
To add to the index in a linked list, the first implementation must go to node in that index ... which is an O (n) operation. Once he got there, the addition is cheap. Thus, adding an index near the beginning (or ending with a smart implementation) is cheap, but adding an index near the middle is expensive.
With an ArrayList flow comes from:
- Copy existing items outside of the index you are adding
- Copying all that buffer is not big enough
source share