JavaScript Exercises: Remove a node at the specified index in a Doubly linked lists

JavaScript Data Structures: Exercise-14 with Solution

Write a JavaScript program that removes the node from the Doubly linked lists at the specified index.

Sample Solution:

JavaScript Code:

``````class Node {
// Constructor function for creating a new node with a given value
constructor(value) {
this.value = value; // Assign the given value to the node
this.next = null; // Initialize the pointer to the next node as null
this.previous = null; // Initialize the pointer to the previous node as null
}
}
// Constructor function for creating a new doubly linked list with a head node having the given value
constructor(value) {
// Initialize the head node with the given value and no next or previous node
value: value, // Store the value of the head node
next: null, // Pointer to the next node in the list, initially set to null
previous: null // Pointer to the previous node in the list, initially set to null
};
this.length = 0; // Initialize the length of the list to 0
this.tail = this.head; // Set the tail node to the head node initially
}
this.head = newNode; // If the list is empty, set the new node as the head
this.tail = newNode; // Also set the new node as the tail
}
else
{
newNode.previous = this.tail; // Set the previous pointer of the new node to the current tail node
this.tail.next = newNode; // Set the next pointer of the current tail node to the new node
this.tail = newNode; // Update the tail node to the new node
}
this.length++; // Increment the length of the list
}
delete(index) {
if (index < 0 || index >= this.length) return null; // Check if the index is valid
if (index === 0) return this.shift(); // If index is 0, remove the first node
if (index === this.length - 1) return this.pop(); // If index is the last one, remove the last node
let current = this.head; // Start from the head of the list
let ctr = 0; // Counter to track the index
while (ctr !== index) { // Iterate until reaching the node to be removed
current = current.next; // Move to the next node
ctr++; // Increment the counter
}
current.previous.next = current.next; // Adjust the next pointer of the previous node
current.next.previous = current.previous; // Adjust the previous pointer of the next node
this.length--; // Decrement the length of the list
return current; // Return the removed node
}
shift() {
if (!this.head) return null; // Check if the list is empty
if (this.length === 1) { // Check if there is only one node in the list
this.tail = null; // Also set tail to null
} else { // If there are more than one node in the list
this.head.previous = null; // Set the previous pointer of the new head to null
old_Head.next = null; // Set the next pointer of the old head to null
}
this.length--; // Decrement the length of the list
}
pop() {
if (!this.tail) return null; // Check if the list is empty
let pNode = this.tail; // Store the reference to the current tail node
if (this.length === 1) { // Check if there is only one node in the list
this.tail = null; // Also set tail to null
} else { // If there are more than one node in the list
this.tail = pNode.previous; // Update the tail to the previous node
this.tail.next = null; // Set the next pointer of the new tail to null
pNode.previous = null; // Set the previous pointer of the old tail to null
}
this.length--; // Decrement the length of the list
return pNode; // Return the removed node (old tail)
}
printList(){

let current = this.head; // Start from the head of the list
let result = []; // Array to store the values of the nodes
while (current !== null) { // Iterate through the list until reaching the end
result.push(current.value); // Push the value of the current node to the array
current = current.next; // Move to the next node
}
console.log(result.join(' ')); // Log the values of the nodes separated by space
return this; // Return the DoublyLinkedList object for chaining
}
}
let numList = new DoublyLinkedList(); // Create a new instance of the DoublyLinkedList class
console.log("Original Doubly Linked Lists:"); // Print the original list
numList.printList();
console.log("Remove the node,index = 5");
numList.delete(5)
numList.printList();
console.log("Remove the node,index = 4");
numList.delete(4)
numList.printList();
console.log("Remove the node,index = 2");
numList.delete(2)
numList.printList();
```
```

Output:

```Original Doubly Linked Lists:
2 3 4 5 6 7
Remove the node,index = 5
2 3 4 5 6
Remove the node,index = 4
2 3 4 5
Remove the node,index = 2
2 4 5
```

Flowchart:

Live Demo:

See the Pen javascript-doubly-linked-list-exercise-14 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus

Linked List Previous: Check if an element is present in a Doubly Linked lists.