androidengineers.Book a session

Linked Lists

Advantages and Drawbacks vs Arrays

article15 minEasy

Choosing between an Array and a Linked List is one of the classic engineering trade-offs in software design. Neither structure is universally superior; each excels under opposite operational patterns.


The Head-to-Head Comparison Matrix

Operational CharacteristicArray (or Dynamic Array)Linked List (Singly / Doubly)Winner
Random Access (get(i))O(1) (Direct arithmetic)O(n) (Sequential walk)Array
Insert / Delete at BeginningO(n) (Must shift all items)O(1) (Update pointer)Linked List
Insert / Delete at EndO(1) amortizedO(1) (with tail pointer)Tie
Insert / Delete in MiddleO(n) (Shift elements)O(1) after pointer is foundLinked List
Search by Value (Unsorted)O(n)O(n)Tie
Search by Value (Sorted)O(log n) (Binary Search)O(n) (Cannot binary search!)Array
Memory OverheadMinimal (consecutive values)High (4–8 bytes per pointer)Array
Cache LocalityExcellent (spatial locality)Poor (pointer hopping)Array
Memory FragmentationRequires large contiguous blockAllocates small scattered nodesLinked List

Why You Almost Never Want LinkedList in Modern Software

In theoretical computer science textbooks, linked lists are praised for O(1) insertions and deletions. However, in modern systems engineering (Android, iOS, backend servers), ArrayList beats LinkedList in 95% of real-world use cases.

The Three Hardware Realities:

  1. To insert in the middle, you must find the position first: While the pointer relink is O(1), searching for index k takes O(k) time. The search cost dominates!
  2. CPU Cache Misses: An array's O(n) memory shift is performed using optimized hardware vector instructions (memmove / System.arraycopy), which operate inside L1 cache lines at gigabytes per second. A linked list's O(n) traversal causes constant RAM stall cycles.
  3. Garbage Collection Overhead: A linked list with 100,000 items creates 100,000 distinct heap objects. This puts massive pressure on the Android Garbage Collector (ART), causing GC pauses and dropped frames. An array list is just 1 heap object!

When SHOULD You Use a Linked List?

  1. Strict O(1) Worst-Case Guarantee: When real-time constraints forbid the occasional O(n) reallocation spike of dynamic arrays.
  2. Frequent Head Insertions / Removals: Implementing Queues or Deques where items enter at one end and leave at the other.
  3. Splitting and Merging Lists: Two linked lists can be spliced together in O(1) time by reconnecting pointers, whereas combining two arrays requires O(n) copying.

Summary

  • Default to Arrays / ArrayLists for cache efficiency, random access, and low GC footprint.
  • Use Linked Lists when splicing lists, implementing queues, or when holding direct node references for O(1) removals (like LRU caches).

YOUR LEARNING JOURNEY

0 of 55 available lessons completed

Progress saved in this browser. No account needed.
Advantages and Drawbacks vs Arrays | Data Structures | Android Engineers