Sobes.tech
Junior — Middle

Which data structure provides faster removal of the last element: ArrayList or LinkedList?

sobes.tech AI

Answer from AI

Removing the last element in a LinkedList is faster than in an ArrayList. In a LinkedList, this is a removal operation from the end of the linked list, which is performed in O(1) time because there is a reference to the last element.

In an ArrayList, removing the last element is also usually quick — amortized O(1), since it simply decreases the size of the list. However, strictly speaking, LinkedList guarantees constant time removal from the end, while ArrayList is amortized.

Example:

ArrayList<Integer> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();

// Removing the last element
arrayList.remove(arrayList.size() - 1); // amortized O(1)
linkedList.removeLast(); // O(1)
Which data structure provides faster removal of the… - sobes.tech