Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Removing Elements from a Double Linked List using Javascript
Removing an element from a doubly linked list involves updating the pointers of adjacent nodes to bypass the node being removed. There are three main cases to consider based on the position of the element.
Three Cases for Removal
- Removing from head: Update head to point to the second node and remove the previous link from the new head node.
- Removing from tail: Update tail to point to the second-to-last node and set its next pointer to null.
- Removing from middle: Connect the previous and next nodes directly, bypassing the current node by updating their pointers.
Visual Representation
Implementation
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
// Insert method for testing
insert(data, position = this.length) {
const newNode = { data, next: null, prev: null };
if (this.length === 0) {
this.head = this.tail = newNode;
} else if (position <= 0) {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
} else if (position >= this.length) {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
} else {
let current = this.head;
for (let i = 0; i < position; i++) {
current = current.next;
}
newNode.next = current;
newNode.prev = current.prev;
current.prev.next = newNode;
current.prev = newNode;
}
this.length++;
}
remove(position = 0) {
if (this.length === 0) {
console.log("List is already empty");
return null;
}
let removedNode;
if (this.length === 1) {
// Only one node
removedNode = this.head;
this.head = this.tail = null;
} else if (position <= 0) {
// Remove from head
removedNode = this.head;
this.head = this.head.next;
this.head.prev = null;
} else if (position >= this.length - 1) {
// Remove from tail
removedNode = this.tail;
this.tail = this.tail.prev;
this.tail.next = null;
} else {
// Remove from middle
let current = this.head;
for (let i = 0; i < position; i++) {
current = current.next;
}
removedNode = current;
current.prev.next = current.next;
current.next.prev = current.prev;
}
this.length--;
return removedNode.data;
}
display() {
if (this.length === 0) {
console.log("List is empty");
return;
}
let result = "";
let current = this.head;
while (current) {
result += current.data + " <-> ";
current = current.next;
}
console.log(result + "null");
}
}
Example Usage
let list = new DoublyLinkedList();
// Insert elements
list.insert(10);
list.insert(20);
list.insert(30);
console.log("Initial list:");
list.display();
// Remove element at position 1 (20)
console.log("\nRemoving element at position 1:");
let removed = list.remove(1);
console.log("Removed:", removed);
list.display();
// Insert 15 at position 1
list.insert(15, 1);
console.log("\nAfter inserting 15 at position 1:");
list.display();
// Remove from head (position 0)
console.log("\nRemoving from head:");
list.remove(0);
list.display();
Initial list: 10 <-> 20 <-> 30 <-> null Removing element at position 1: Removed: 20 10 <-> 30 <-> null After inserting 15 at position 1: 10 <-> 15 <-> 30 <-> null Removing from head: 10 <-> 15 <-> 30 <-> null
Key Points
- Always check if the list is empty before attempting removal
- Handle the special case when removing the only node in the list
- Update both next and prev pointers when removing from middle positions
- Remember to decrement the length counter after successful removal
- Return the removed data for potential use by the caller
Conclusion
Removing elements from a doubly linked list requires careful pointer management to maintain the bidirectional links. The key is identifying the removal case and updating the appropriate pointers to bypass the removed node.
Advertisements
