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
| Operation | Array | Linked List |
|---|---|---|
| Access by index | O(1) | O(n) |
| Insert at head | O(n) | O(1) |
| Insert at tail | O(1) amortized | O(1) with tail pointer |
| Insert in middle | O(n) | O(1) with node reference |
| Delete at head | O(n) | O(1) |
| Search | O(n) | O(n) |
| Memory overhead | None | One 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
- Open the Linked List Visualizer
- Use Insert Head, Insert Tail, or Insert After to add nodes — enter a value and click
- Click Delete Head or Delete Tail to remove nodes, or enter a value and click Delete Value
- Click Search to highlight a node by value
- Use Reverse to reverse the entire list and watch pointers flip
- 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).
| Feature | Singly | Doubly |
|---|---|---|
| Memory per node | value + 1 pointer | value + 2 pointers |
| Reverse traversal | Not possible | O(n) |
| Delete given node | O(n) — must find prev | O(1) |
| Implementation complexity | Simpler | More edge cases |
| Used in | Stacks, simple queues | Browser 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