PublicSoftTools
Tools6 min read

Linked List Visualizer — Understand Nodes and Pointers

A linked list stores data in nodes connected by pointers — each node holds a value and a reference to the next. This guide explains singly and doubly linked lists, covers every core operation, and shows how to use the interactive visualizer to build intuition for pointer manipulation.

Linked List vs Array: When to Use Which

OperationArrayLinked List
Access by indexO(1)O(n)
Insert at headO(n)O(1)
Insert at tailO(1) amortizedO(1) with tail pointer
Insert in middleO(n)O(1) with node reference
Delete at headO(n)O(1)
SearchO(n)O(n)
Memory overheadNoneOne pointer per node

Linked lists win when insertions and deletions at arbitrary positions are frequent and you already hold a reference to the node. Arrays win for random access and cache performance (contiguous memory).

How to Use the Linked List Visualizer

  1. Open the Linked List Visualizer
  2. Use Insert Head, Insert Tail, or Insert After to add nodes — enter a value and click
  3. Click Delete Head or Delete Tail to remove nodes, or enter a value and click Delete Value
  4. Click Search to highlight a node by value
  5. Use Reverse to reverse the entire list and watch pointers flip
  6. Toggle between Singly and Doubly mode to see both pointer directions

Singly vs Doubly Linked Lists

In a singly linked list, each node has one pointer: next. Traversal is one-directional — forward only. In a doubly linked list, each node carries both next and prev pointers, enabling bidirectional traversal and O(1) deletion when given a node reference (no need to find the predecessor).

FeatureSinglyDoubly
Memory per nodevalue + 1 pointervalue + 2 pointers
Reverse traversalNot possibleO(n)
Delete given nodeO(n) — must find prevO(1)
Implementation complexitySimplerMore edge cases
Used inStacks, simple queuesBrowser history, LRU cache

Core Operations Explained

Insert at head

Create a new node, set its next to the current head, then update head to point to the new node. Three pointer assignments, O(1) always. This is why linked lists make better stacks than arrays when memory allocation is expensive.

Delete a node by value

Traverse from head, keeping a reference to the previous node. When you find the target, set prev.next = target.next. The target node is now unreferenced and eligible for garbage collection. Edge case: deleting the head requires updating the head pointer itself.

Reversing a linked list

The classic interview problem. Maintain three pointers: prev = null, current = head, next = null. In each iteration: save current.next, point current.next to prev, advance both prev and current. After the loop, prev is the new head. O(n) time, O(1) space.

Detecting a cycle (Floyd's algorithm)

Use two pointers: a "slow" pointer that advances one step at a time and a "fast" pointer that advances two steps. If they ever point to the same node, a cycle exists. If the fast pointer reaches null, no cycle. This runs in O(n) time with O(1) space — the visualizer shows the straight-line case; cycles are a manual extension.

Common Interview Questions

Find the middle of a linked list in one pass

Use the slow/fast pointer trick: move slow one step and fast two steps. When fast reaches the end, slow is at the midpoint. For even-length lists, slow lands at the second middle node.

Why can't you do binary search on a linked list?

Binary search requires O(1) access to the midpoint. On a linked list, finding the midpoint requires O(n) traversal, making the total complexity O(n log n) — worse than a linear scan. Binary search only makes sense on arrays or skip lists.

What is a sentinel node and why use it?

A sentinel (dummy) head node always exists at the start of the list but holds no meaningful value. It eliminates special-case handling for empty lists and head insertions/deletions — every operation always has a previous node to update. Widely used in production linked list implementations.

Try the Linked List Visualizer

Build and modify linked lists step by step in the Linked List Visualizer — insert, delete, reverse, and search with animated pointer updates.

Open Linked List Visualizer