Browse curriculum

Linked List Overview

Master the structure of Nodes and Pointers.

Easy
10 min

What is a Linked List?

A Linked List is a linear data structure where elements are stored in nodes. Unlike arrays, nodes are not stored in contiguous memory locations; instead, each node contains a pointer (reference) to the next node in the sequence.

The Motivation: Dynamic Memory

Arrays have fixed sizes. If you need to insert an element in the middle of a large array, you have to shift all subsequent elements (O(N)). Linked Lists allow O(1) insertion and deletion at a node you already hold a reference to, and they grow one node at a time without reallocating a large block of memory.

Visualizing the Structure

Imagine a treasure hunt. Each clue (Node) has a piece of information (Data) and the location of the next clue (Pointer). You can't skip to the end; you must follow the trail from the start (Head).

When should I use a Linked List?

  • Dynamic Size: When you don't know the number of elements in advance.
  • Frequent Insertions/Deletions: Especially at the beginning or middle.
  • Implementing Stacks/Queues: Easy to add/remove from ends.

The Intuition: Chasing Pointers

Navigating a linked list is about moving from one node to the next using current = current.next. You must always ensure you don't lose the reference to the head, and be careful not to access properties of null (the end of the list).

Interactive Visualization

5 NODES · HEAD → … → NULL

CURRENT = HEAD

head current

3

#0

→

1

#1

→

4

#2

→

1

#3

→

5

#4

→ NULL

current

3

Printed

0

printed

Nothing yet

 

current = head: the node holding 3

Next

current is not NULL — it holds 3, so keep going

Values

Legend & Complexity

current: the node being read

Already printed

The next compartment: where the pointer lives

→ a next pointer; → NULL ends the list

Traversal

O(n)

Access by index

O(n)

Space

O(1)

Walk a list the only way a linked list allows: start at head, print current.val, then follow current = current.next until current is NULL. Each node is a value and a next pointer, and the code panel lights the line that is running.

Basic Operations Template

A node definition, and the traversal loop every other linked-list technique builds on.

Linked List Overview Code
1function ListNode(val, next) {
2 this.val = (val===undefined ? 0 : val)
3 this.next = (next===undefined ? null : next)
4}
5
6// Traversal
7let current = head;
8while (current) {
9 console.log(current.val);
10 current = current.next;
11}

Edge Cases & Common Mistakes

  • Null Head: Always check if head is null before accessing properties.
  • Lost Reference: If you change head without saving it, you lose the whole list.
  • Cycles: Iterating forever if the list loops back on itself.

Summary

Linked Lists are fundamental for understanding pointers and references. They trade the speed of random access for the flexibility of dynamic insertion and deletion.

The approach, step by step

  1. Start at head

    Set current to the head of the list.

  2. Process the node

    While current is not null, read or print current.val.

  3. Follow the pointer

    Move to current.next, and stop when current is null.

Frequently asked questions

What is a linked list?

A sequence of nodes where each node stores a value and a reference to the next node. The list is reached through its head, and the last node’s next is null.


How do you traverse a linked list?

Start a pointer at head. While it is not null, process the node’s value and move the pointer to node.next. Each node is visited once, in link order.


Linked list vs array: when is a linked list better?

A linked list inserts or deletes at a node you already hold in O(1) and grows without reallocating, but reaching the i-th element takes O(n). An array gives O(1) access by index but shifts elements to insert in the middle.

Want to see the mechanism first? Step through it in the visualizer:

Singly Linked List

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description