Skip to main content

DSAPath12 mincore

Array vs Linked List

Arrays give indexed access and cache-friendly scans; linked lists make node insertion and splicing cheap when you already have the node.

DifficultyCore
TierTier 1
ModuleComparisons
LanguagesPython

Why This Matters

Array vs linked list is not a trivia question. It is a representation choice that changes indexing, traversal, memory layout, and update cost.

Decision Rule

Use an array or dynamic array when you need fast indexed access, compact storage, or repeated scanning. Use a linked list when you already hold node references and need cheap local insertion, deletion, or splicing.

Comparison Table

NeedArray / dynamic arrayLinked list
Access by indexO(1)O(n)
Scan all elementsO(n), cache-friendlyO(n), pointer chasing
Insert at endamortized O(1) for dynamic arrayO(1) with tail pointer
Insert in middleO(n) shift after finding indexO(1) after finding node
Memory overheadlowextra pointers per node
Typical DSA usetwo pointers, binary search, heapsLRU links, adjacency nodes, splicing

Worked Example

Suppose you need the 50,000th item many times. An array returns it by address calculation. A linked list must follow 50,000 links each time.

Now suppose you have a pointer to a node in the middle and need to remove it. A doubly linked list can relink neighbors in constant time. An array must shift every later element.

Common Mistakes

MistakeCorrection
Saying linked-list insertion is always O(1).Finding the insertion point is often O(n); only the relink is O(1).
Ignoring cache locality.Arrays often beat linked lists in practice for scans because memory is contiguous.
Using a linked list for binary search.Binary search needs efficient middle access, so arrays fit better.

Diagnostic Questions

QuestionAnswer signal
Which structure supports arr[i] in constant time?Array or dynamic array.
Is deleting a known doubly linked-list node O(1)?Yes, if the node and neighbor links are available.
Why can an array scan be faster than a linked-list scan?Contiguous memory gives better cache behavior.

Exercises

  • Beginner: Explain why binary search needs an array-like structure.
  • Beginner: Give one problem where linked-list splicing is useful.
  • Beginner: Mark each operation as array-friendly or linked-list-friendly: random access, stack push, middle splicing.
  • Intermediate: Implement a tiny LRU cache using a hash table plus doubly linked list.
  • Intermediate: Compare Python list and collections.deque for appending at both ends.
  • Challenge: Benchmark scanning one million integers in a Python list versus walking a linked node structure.

References