Skip to content
Go back

Understanding Linked Lists (DSA Series)

Updated:
Understanding Linked Lists (DSA Series)

Note (2025) This post is part of a brief CS Fundamentals / DSA series. Linked lists rarely appear in day-to-day Rails/web work, but they’re still foundational for interviews and for understanding how higher-level containers work under the hood. If you prefer strictly application-engineering topics, feel free to skip—but if you’re brushing up for interviews or low-level systems work, this might help.


Table of Contents

Open Table of Contents

What is a linked list?

A linked list is a linear collection of elements (nodes) where each node holds:

Unlike arrays, linked lists don’t require contiguous memory; nodes can live anywhere in memory, connected by references.

Flavors


Why (and when) to use one?

Pros

Cons

Typical uses

Core Operations (singly linked list)

Below is minimal, language-agnostic pseudocode; adapt easily to Ruby, Python, or JS.

Node

class Node:
  value
  next

Insert at head — O(1)

def push_front(head, value):
  node = Node(value)
  node.next = head
  return node  # new head

Insert after a node — O(1)

def insert_after(node, value):
  new_node = Node(value)
  new_node.next = node.next
  node.next = new_node

Delete after a node — O(1)

def delete_after(node):
  if node == null or node.next == null: return
  node.next = node.next.next

Search — O(n)

def find(head, target):
  cur = head
  while cur != null:
    if cur.value == target: return cur
    cur = cur.next
  return null

Doubly Linked List essentials

A DLL node stores prev and next:

class Node:
  value
  prev
  next

Benefits

Trade-off

Complexity & Trade-offs

OperationSLLDLLArray (dynamic)
Access k-th elementO(n)O(n)O(1)
Insert at headO(1)O(1)O(n) (shift)
Insert at tail (with tail ptr)O(1)O(1)Amortized O(1)
Delete given a node refO(1)O(1)O(n) (shift)
Memory overhead per elementpointer2 pointersnone extra
Cache friendlinesslowerlowerhigher

Rule of thumb: If you need frequent insertions/removals at the ends or you maintain node references, lists shine. For random access by index, arrays win.


When not to use a linked list

Modern libraries often give you the right structure already (e.g., Deque, LinkedList, OrderedDict in various languages), implemented efficiently and tested—use them first.


In the real world (2025)


Practice prompts


Wrap-up

Linked lists are a conceptual cornerstone. Even if you rarely write one from scratch in production, understanding their costs and benefits helps you pick the right container—and ace common interview tasks.


Share this post on:

Previous Post
WeakMaps in JavaScript
Next Post
Centering a Fixed-Sized Element with CSS