In this post, we will learn the difference between Linked List and Doubly Linked List in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Difference between Linked List and Doubly Linked List in Java
Linked List |
Doubly Linked List |
In a Linked List, each node points to the next node in the sequence. |
In a Doubly Linked List, each node points to both the next node and the previous node in the sequence. |
Linked List only allows traversal in one direction (i.e., forward). |
Doubly Linked List allows traversal in two directions (i.e., both forward and backward). |
Linked List requires less memory per node as it only needs to store the address of the next node. |
Doubly Linked List requires more memory per node as it needs to store the addresses of both the next and the previous nodes. |
Insertion or deletion of a node in the Linked List requires the traversal from the head node to the required node. |
Insertion or deletion in a Doubly Linked List can be more efficient if the node to be deleted is given as you can move either from the beginning or from the end as applicable. |
The last node of a Linked List has the next pointer pointing to null. |
The next pointer of the last node in a Doubly Linked List points to null, as does the previous pointer of the head node. |
Operations like inserting a node before a given node or removing the previous node are expensive operations in Linked List as you have to traverse from the head to find the previous node. |
Operations like inserting a node before a given node or removing the previous node are more efficient in Doubly Linked List because of the previous pointer. |
Examples in Java: java.util.Linked List (it's internally a doubly linked list, but you can use it as a singly linked list in terms of operations). |
Examples in Java: java.util.Linked List. |
References
Comments
Post a Comment