Doubly Linked List in Data Structure with Example
A Doubly Linked List is a linked list variant where each node holds references to both the next and previous nodes, allowing traversal in both directions.
Node Structure and Implementation
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = self.tail = new_node
return
new_node.prev = self.tail
self.tail.next = new_node
self.tail = new_node
def delete(self, node):
if node.prev:
node.prev.next = node.next
else:
self.head = node.next
if node.next:
node.next.prev = node.prev
else:
self.tail = node.prev
Example
Appending 10, 20, 30 to an empty doubly linked list produces: 10 <-> 20 <-> 30, with node 20's prev pointing to 10 and next pointing to 30, allowing traversal from either end.
Complexity
| Operation | Time |
|---|---|
| Insert at head/tail | O(1) |
| Delete (given node reference) | O(1) |
| Search | O(n) |
Applications
- Browser forward/backward navigation history.
- Implementing LRU (Least Recently Used) caches.
- Music players with next/previous track functionality.
- Text editors supporting undo/redo in both directions.
PreviousLinked List in Data Structure — Types and Applications
Next Circular Linked List in Data Structure
Ready to master Data Structures & Algorithms?
Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.
.png)