Array class with the following method
- insert(int item)
- removeAt(int index)
- indexOf(int item)
- max()
- intersect()
- reverse()
- insertAt(int item, int index)
- Vector - when full grow 100% of it size, synchronous (single threaded)
- ArrayList - when full grow 50% of it size, not synchronous
Lookup by index O(1), Lookup by value O(n), Insert O(n), Delete O(n)
Classes needed
- LinkedList
- Node(int value)
LinkedList class with following method
- addLast(int item); O(1)
- addFirst(int item); O(1)
- indexOf(int item); O(n)
- contains(int item); O(n)
- removeFirst(); O(1)
- removeLast(); O(n)
- size(); O(1) / O(n) Complexity based on implementation of this method
- toArray(); O(n)
- reverse(); O(n)
- getKthNodeFromTheEnd(int k); O(n)
Space complexity
- Static array have fixed size
- Dynamic grow by 50-100%
- Link list dose not waste space
- Use array if know before hand the size of array;
- new ArrayList(100)
Runtime complexity
Factor | Array | LinkedList |
---|---|---|
Lookup by Index | O(1) | O(n) |
Lookup by value | O(n) | O(n) |
Insert at beginning | O(n) | O(1) |
Insert at end | O(n) | O(1) |
Insert at middle | O(n) | O(n) |
Delete at Beginning | O(n) | O(1) |
Delete at End | O(n) | O(n) |
Delete at middle | O(n) | O(n) |
Types of LinkedList
- Single
- Double
- Circular