What is the difference between ArrayList and LinkedList?

Beginner

Answer

ArrayList:

  • Dynamic array implementation
  • Fast random access O(1)
  • Slow insertion/deletion in middle O(n)
  • Better for frequent access operations
    LinkedList:
  • Doubly-linked list implementation
  • Slow random access O(n)
  • Fast insertion/deletion O(1)
  • Better for frequent modification operations
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
// ArrayList better for
String element = arrayList.get(100); // O(1)
// LinkedList better for
linkedList.add(0, "first"); // O(1)
linkedList.remove(0);       // O(1)