In a doubly linked list every node stores three things: a pointer to the previous node, the value and a pointer to the next node. The list keeps both a head and a tail pointer, so you can walk it in either direction and add to either end in constant time. This visualizer draws every node at its memory address with both pointer fields, solid arrows for next and dashed purple arrows for prev.
Run an operation and watch exactly which pointers change and in which order: inserting in the middle touches four pointers, deleting a node needs no search for the previous node, and reversing simply swaps next and prev in every node. The code panel highlights each pseudocode line and has full C++, Java, Python and JavaScript versions.
How to use the Doubly Linked List Visualizer tool
- Enter a value and, for insert at index, an index.
- Choose an operation: insert at head, tail or index, delete value, search value or reverse.
- Watch the arrows: solid black arrows are next pointers, dashed purple arrows are prev pointers, red shows the pointer that just changed.
- Step forward and back to study each pointer update, or switch to the List order view.
Why use ToolOrca Doubly Linked List Visualizer
- Nodes with prev, value and next fields at real addresses
- Separate arrows for next and prev pointers
- Head and tail pointers shown
- O(1) insert at both ends
- Delete without walking for the previous node
- Reverse by swapping pointers
- Pseudocode and code in four languages
How a doubly linked list works
Each node has prev and next pointers. The first node’s prev is NULL and the last node’s next is NULL. Because the list keeps a tail pointer as well as a head, adding or removing at either end takes O(1): no walking is needed.
The extra pointer makes deletion easier. In a singly linked list you need the node before the one you delete, so you walk with a prev pointer. In a doubly linked list the node already knows its neighbours: set cur.prev.next = cur.next and cur.next.prev = cur.prev, and it is unlinked.
Inserting in the middle: four pointers
To insert a node after cur: set node.next = cur.next and node.prev = cur, then cur.next.prev = node (or tail = node at the end) and finally cur.next = node. Doing these in the wrong order can lose part of the list, which is why the visualizer shows each pointer change separately.
Reversing a doubly linked list is simple: walk the list and swap next and prev in every node, then swap head and tail. It is O(n) time and O(1) extra space.
Where doubly linked lists are used
Doubly linked lists power LRU caches (a hash map plus a doubly linked list), browser back and forward history, undo and redo, music playlists, and the std::list and LinkedList classes in C++ and Java. The cost is one extra pointer per node and a few more pointer updates per operation.
Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.
Frequently asked questions
What is the difference between a singly and a doubly linked list?
A singly linked node has only a next pointer, so you can only move forward. A doubly linked node also has a prev pointer, so you can move both ways and delete a node without searching for the one before it.
Is deletion O(1) in a doubly linked list?
Deleting a node you already have a pointer to is O(1). Finding the node by value first is still O(n) because you have to search for it.
How much extra memory does a doubly linked list use?
One more pointer per node: 8 bytes on a 64-bit system. For small values such as ints, the pointers can take more memory than the data.
How do you reverse a doubly linked list?
Swap the next and prev pointers of every node, then swap the head and tail pointers.