A Linked List is a linear data structure where each element has a pointer pointing to the next, forming a sequence of elements. Unlike an array, elements in the linked list are not stored in contiguous memory locations; instead, they are stored at random locations connected through pointers.
In this article, we will explore the advantages, disadvantages, and uses of doubly-linked lists.
Also, read - Merge sort in linked list
Advantages of Doubly Linked List (DLL)
The following are the advantages of Doubly Linked List:
- Reversing a doubly linked list is very easy: We only need to swap each element’s next and previous pointers and update the head element to point to the last element to reverse a doubly linked list.
- Nodes can be easily accessed in both directions: It allows traversing in both forward and backward directions because of the next and previous pointers, unlike the singly linked list, which allows traversing in only one direction.
- Easy deletion of nodes: Deletion of elements is more straightforward compared to a singly linked list. This is because to delete an element, we need access to the element to be deleted and the element previous to it. So, we require two pointers for deletion in a singly linked list. In contrast, in a doubly-linked list, there is no need to maintain an extra pointer as each element carries the information of the previous element.
- We can easily insert a new element before a given element because we have information for both the previous and next element so we can update it accordingly.
- It is very easy to implement.
Disadvantages of Doubly Linked List (DLL)
The following are the disadvantages of Doubly Linked List:
- It consumes extra memory space compared to a singly linked list due to the additional previous pointer it has to maintain for each element.
Note: This disadvantage can be overcome using XOR-linked lists. - It is impossible to access the list’s elements directly because they are stored at random locations, and we can only access them sequentially.
- All the operations require more time due to the overhead of handling extra pointers. For instance, if you have to insert a new element, you must modify the previous pointers and the next pointers, similarly in the case of deletion.