Advantages of Doubly Linked List Over Singly Linked List
A doubly linked list is an extension of a singly linked list in which each node points to both its next and previous nodes. This structure allows traversal in both forward and backward directions. Let’s discuss the main reasons why a Doubly Linked List is preferred over a Singly Linked List.
Reason 01: Bidirectional Traversal
In Singly Linked List, the each node can be traversed in both forward and backward directions using next
and prev
pointers.
In a doubly linked list, Traversal is only possible in one direction.
Doubly linked list backward traversal is essential in applications like undo operations or navigating in both directions (e.g., browser history).
Reason 02: Deletion operation is faster in DLL
Consider a case where a pointer to the intermediate node is already given, and we need to delete that node in both singly and doubly linked lists.
In case of Singly Linked List traversal is overhead, we need one more pointer pointing to the node just before the one we want to delete. This helps update the next
pointer to skip the target node.
- Example: Right now, the pointer `ptr2` and ‘head‘ is pointing to the first node in the list. However, if we have seven nodes and our goal is to delete the 6th node, `ptr2` will need to move five steps forward to reach just before it. This means we need to traverse the list.
Deleting a node doubly linked list is straightforward. There is no need to maintain an extra pointer to remove the node, as it is always possible to access the previous node directly from the node that is to be deleted.
- This means that we do not need to traverse the list in DLL.
Hence proved that, deletion operation is faster in doubly linked list over the singly linked list
Important: If you need more help to understand that, how the traversal is actually happened for insertion and deletion in singly linked list follow the given link please.
Reason 03: Inserting a new node before a given node is easier in DLL
In case of Singly Linked List traversal is overhead, we need a pointer to the previous node in order to add a new node before the given node. Similar to deletion.
- Example: Currently, the pointer `ptr2` and ‘head’ are pointing to the first node of the list. If we have seven nodes and our goal is to add a new node before the 6th node, we need a pointer to that point to 5th node which is just before the given node. This requires lot of effort for traversing in list.
In case of doubly linked list, inserting a new node before the given node is very easy because of bidirectional of each node feature.
CONCLUSION: When it is necessary to access the previous node from a given node without traversing the list, a doubly-linked list proves to be very useful.
Reason 04: Reversing in DLL is Easy
In Singly Linked List (SLL): Reversing is complex because each node only points to the next one. You need extra pointers and careful handling of these pointers to avoid losing the structure of the list.
In Doubly Linked List (DLL): Each node in DLL has two pointers: next
(points to the next node) and prev
(points to the previous node). To reverse the list:
- Traverse through the list, and for each node, swap the
next
andprev
pointers. - At the end of the traversal, update the head to point to the last node.
That’s why DLL is simpler in reversing