Doubly & Circular Linked Lists with Sentinel/Dummy Nodes
Doubly & Circular Linked Lists with Sentinel/Dummy Nodes
While Singly Linked Lists only allow unidirectional forward traversal, advanced system architectures (like LRU Caches and OS process schedulers) require bidirectional navigation and circular loops. Doubly Linked Lists provide both prev and next pointers, while Sentinel (Dummy) Nodes eliminate treacherous null-pointer edge cases.
1. Real-World Analogy: Train Carriages & Playlists
- Doubly Linked List (Train Carriages): Each train car is physically hooked to both the car in front (Prev) and the car behind (Next). A conductor can walk forward or backward between cars freely.
- Circular Linked List (Music Playlist on "Repeat"): When the last song (Tail) finishes, its
nextpointer loops straight back to the first song (Head), creating an endless seamless cycle.
2. Why Sentinel (Dummy) Nodes Are an Industry Standard
When modifying linked lists (inserting/deleting), handling deletions at the absolute head or tail usually requires verbose, bug-prone if (head == null) checks.
A Sentinel / Dummy Node is an empty placeholder node that permanently anchors the beginning and end of the list:
With dummy nodes:
- The list is never empty (always contains at least Head and Tail sentinels).
- Every real data node always has both a valid previous and next neighbor.
- Edge cases for empty lists, single-node lists, and boundary deletions disappear completely!
3. Designing a Doubly Linked List Node
Removing a Node in $O(1)$ Time Given its Reference:
In a singly linked list, deleting a node requires $O(n)$ traversal to find its predecessor. In a doubly linked list, because the node knows its own prev, deletion is an instant $O(1)$ pointer splice:
4. Multi-Language Implementations: Doubly Linked List with Sentinels
Python 3:
Modern JavaScript / TypeScript:
5. Circular Linked Lists
In a Circular Singly Linked List, the tail node points back to head instead of null:
- Traversal Guard: Loops cannot use
while curr != null. You must checkwhile curr.next != headto prevent infinite loops! - Top Applications: Round-robin CPU time-slice scheduling, multiplayer turn-based board games.
Multiple Choice Questions
Fast & Slow Pointers: Floyd's Cycle Detection & Middle Node Finding
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.