Opening the book…
The following table summarizes the differences between MyLinkedList and MyArrayList, where 1 means \( O(1) \) or constant time and n means \( O(n) \) or linear.
|
MyArrayList |
MyLinkedList |
|
|---|---|---|
|
add (at the end) |
1 | n |
| add (at the beginning) | n | 1 |
| add (in general) | n | n |
| get / set | 1 | n |
| indexOf / lastIndexOf | n | n |
| isEmpty / size | 1 | 1 |
| remove (from the end) | 1 | n |
| remove (from the beginning) | n | 1 |
| remove (in general) | n | n |
The operations where MyArrayList has an advantage are adding at the end, removing from the end, getting and setting.
The operations where MyLinkedList has an advantage are adding at the beginning and removing from the beginning.
For the other operations, the two implementations are in the same order of growth.
Which implementation is better? It depends on which operations you are likely to use the most. And that’s why Java provides more than one implementation, because it depends.
This page titled 4.2: Comparing MyArrayList and MyLinkedList was last modified on Tue, 01 Dec 2020 03:50:40 GMT and is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .
The book could not be loaded just now.