Doubly Linked List in JavaScript
A Doubly Linked List (DLL) contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in the singly linked list.
1 Representation of a DLL Node
class Node {
constructor(data = null) {
this.data = data;
this.next = null;
this.prev = null;
}
setter(x) {
this.data = x;
}
}2 Singly vs Doubly Linked List (Advantages & Disadvantages)
| Feature | Singly Linked List (SLL) | Doubly Linked List (DLL) |
|---|---|---|
| Node Structure | SLL nodes contain 2 fields: data field and next link field. | DLL nodes contain 3 fields: data field, previous link field, and next link field. |
| Direction | Traversal can be done using the next node link only (one direction). | Traversal can be done using previous or next node links (forward and backward). |
| Memory | Occupies less memory as it has only 2 fields. | Occupies more memory as it has 3 fields. |
| Insertion/Deletion | Complexity at a given position is O(n). | Complexity at a given position is O(n/2) = O(n) because traversal can be made from start or end. |
| Node Deletion | Complexity of deleting a given node is O(n) because the previous node needs to be known. | Complexity of deleting a given node is O(1) because the previous node can be accessed easily using the prev pointer. |
| Use Cases | Preferred for execution of stacks. | Preferred to execute heaps, stacks, and binary trees. |
Advantages of DLL over SLL:
- A DLL can be traversed in both forward and backward directions.
- The delete operation in DLL is more efficient if a pointer to the node to be deleted is given.
- We can quickly insert a new node before a given node.
- In a singly linked list, to delete a node, a pointer to the previous node is needed. In DLL, we can get the previous node using the previous pointer.
Disadvantages of DLL over SLL:
- Every node of DLL requires extra space for a previous pointer.
- All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with the next pointers.
3 Insert at Begin of Doubly Linked List
The new node is always added before the head of the given Linked List. And the newly added node becomes the new head of the DLL.
Time Complexity: O(1)
Auxiliary Space: O(1)
function insertAtFront(head, newData) {
let newNode = new Node();
newNode.setter(newData);
newNode.next = head;
newNode.prev = null;
if (head !== null) {
head.prev = newNode;
}
head = newNode;
return head;
}4 Insert at End Doubly Linked List
There are two cases:
- Insertion in an empty list: The new node becomes the head.
- List initially contains some nodes: Start points to the first node, and we must traverse to the last node to attach the new node.
Time Complexity: O(N) for traversing to the end. (If a tail pointer is maintained, it becomes O(1)).
function insertAtEndDLL(head, newData) {
let newNode = new Node();
newNode.setter(newData);
if (head === null) {
newNode.prev = null;
head = newNode;
return head;
}
let last = head;
while (last.next !== null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
return head;
}5 Reverse a Doubly Linked List
Given a Doubly Linked List, the task is to reverse it. Follow the given steps to solve the problem using the above approach:
- Traverse the linked list using a pointer.
- Swap the
prevandnextpointers for all nodes. - At last, change the head pointer of the doubly linked list.
Time Complexity: O(N), where N denotes the number of nodes in the doubly linked list.
Auxiliary Space: O(1)
function reverseDLL(head) {
let temp = null;
let curr = head;
// Swap next and prev for all nodes
while (curr !== null) {
temp = curr.prev;
curr.prev = curr.next;
curr.next = temp;
curr = curr.prev; // Move to the next node (which is currently stored in prev!)
}
// Before changing head, check for the cases like empty list and list with only one node
if (temp !== null) {
head = temp.prev;
}
return head;
}6 Delete Head of a Doubly Linked List
Write a function to delete the head node in a doubly-linked list.
Time Complexity: O(1)
function deleteHead(head) {
if (head === null) return null;
if (head.next === null) {
return null; // Only one node in the list
}
head = head.next;
head.prev = null;
return head;
}7 Delete a given node in a Doubly Linked List
Write a function to delete a given node in a doubly-linked list. This approach covers the deletion of the head node, middle node, and last node.
Algorithm:
- Let the node to be deleted be
del. - If the node to be deleted is the head node, then change the head pointer to the next current node:
if (head === del) { head = del.next; } - Set
prevof the next node todel, if the next node exists:if (del.next !== null) { del.next.prev = del.prev; } - Set
nextof the previous node todel, if the previous node is not null:if (del.prev !== null) { del.prev.next = del.next; }
Time Complexity: O(1) - Since traversal of the linked list is not required, the time complexity is constant.
Space Complexity: O(1) - No extra space is required, so the space complexity is constant.
function deleteNode(head, del) {
// Base case
if (head === null || del === null) {
return head;
}
// If node to be deleted is head node
if (head === del) {
head = del.next;
}
// Change next only if node to be deleted is NOT the last node
if (del.next !== null) {
del.next.prev = del.prev;
}
// Change prev only if node to be deleted is NOT the first node
if (del.prev !== null) {
del.prev.next = del.next;
}
return head;
}