FMTechnical notesPortfolio
Back to the blog

Computer Science · Intermediate

Linked lists: data connected node by node

A linked list does not store contiguous elements: every node keeps a value and a reference to the next one. That freedom changes insertion and access costs.

After reading this
  • Understand references
  • Insert without shifting elements
  • Compare with arrays
01 / 03

The minimal node

The list knows its head. From there, each reference leads to the next node until null.

ts
type Node<T> = { value: T; next: Node<T> | null };

const head: Node<number> = {
  value: 10,
  next: { value: 20, next: null }
};
02 / 03

Front insertion is constant

Creating a node that points to the current head and replacing the head costs O(1). Finding position twenty still costs O(n).

js
function prepend(list, value) {
  return { value, next: list };
}
03 / 03

When to use it

It helps with frequent insertion or deletion when you already have the right reference. For random access, an array is generally better and cache-friendly.

Linked-list builder● interactive
clientepedidopagoNULL
Practice

Reverse a list

Traverse a list and reverse every reference without creating another array.

Show a hint

Keep previous, current, and next.