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
How to Delete a Linked List in JavaScript?
In this article, we are going to explore Linked List and how to delete a linked list in JavaScript.
A Linked List is a linear data structure where elements are not stored in contiguous memory locations. Each element (called a node) contains data and a pointer/reference to the next node in the sequence.
Node Structure
First, let's define the basic structure of a node in our linked list:
<script>
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Example: Creating a single node
let node = new Node(10);
console.log("Node data:", node.data);
console.log("Node next:", node.next);
</script>
Methods to Delete a Linked List
Method 1: Simple Reference Deletion
The simplest way to delete an entire linked list in JavaScript is to set the head reference to null. JavaScript's garbage collector will automatically clean up the unreachable nodes.
<script>
class LinkedList {
constructor() {
this.head = null;
}
// Add node at the beginning
push(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
// Simple deletion method
deleteList() {
this.head = null;
console.log("Linked list deleted successfully");
}
// Display the list
display() {
if (this.head === null) {
console.log("List is empty");
return;
}
let current = this.head;
let result = "";
while (current !== null) {
result += current.data;
if (current.next !== null) {
result += " -> ";
}
current = current.next;
}
console.log(result);
}
}
// Example usage
let list = new LinkedList();
list.push(3);
list.push(2);
list.push(1);
console.log("Before deletion:");
list.display();
list.deleteList();
console.log("After deletion:");
list.display();
</script>
Before deletion: 1 -> 2 -> 3 Linked list deleted successfully After deletion: List is empty
Method 2: Iterative Node-by-Node Deletion
For educational purposes or in languages without garbage collection, you might want to explicitly traverse and delete each node:
<script>
class LinkedList {
constructor() {
this.head = null;
}
push(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
// Explicit node-by-node deletion
deleteListIteratively() {
let current = this.head;
let nodeCount = 0;
while (current !== null) {
let next = current.next;
current.data = null;
current.next = null;
current = next;
nodeCount++;
}
this.head = null;
console.log(`Deleted ${nodeCount} nodes iteratively`);
}
display() {
if (this.head === null) {
console.log("List is empty");
return;
}
let current = this.head;
let result = "";
while (current !== null) {
result += current.data;
if (current.next !== null) {
result += " -> ";
}
current = current.next;
}
console.log(result);
}
}
// Example usage
let list2 = new LinkedList();
list2.push(5);
list2.push(4);
list2.push(3);
list2.push(2);
list2.push(1);
console.log("Before iterative deletion:");
list2.display();
list2.deleteListIteratively();
console.log("After iterative deletion:");
list2.display();
</script>
Before iterative deletion: 1 -> 2 -> 3 -> 4 -> 5 Deleted 5 nodes iteratively After iterative deletion: List is empty
Complete Example with Multiple Operations
<script>
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
push(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
this.size++;
}
getSize() {
return this.size;
}
isEmpty() {
return this.head === null;
}
deleteList() {
this.head = null;
this.size = 0;
console.log("Complete linked list deleted");
}
display() {
if (this.isEmpty()) {
console.log("List is empty");
return;
}
let current = this.head;
let elements = [];
while (current !== null) {
elements.push(current.data);
current = current.next;
}
console.log("List: " + elements.join(" -> "));
}
}
// Demonstration
let myList = new LinkedList();
// Build the list: 1->4->1->12->1
[1, 12, 1, 4, 1].forEach(value => myList.push(value));
console.log("Elements in List Before Deletion:");
myList.display();
console.log("List size:", myList.getSize());
console.log("\nDeleting the list...");
myList.deleteList();
console.log("\nElements in List After Deletion:");
myList.display();
console.log("List size:", myList.getSize());
console.log("Is list empty?", myList.isEmpty());
</script>
Elements in List Before Deletion: List: 1 -> 4 -> 1 -> 12 -> 1 List size: 5 Deleting the list... Complete linked list deleted Elements in List After Deletion: List is empty List size: 0 Is list empty? true
Key Points
- Simple Approach: Setting head to null is sufficient in JavaScript due to automatic garbage collection
- Memory Management: JavaScript automatically frees memory of unreachable objects
- Time Complexity: O(1) for simple deletion, O(n) for iterative deletion
- Space Complexity: O(1) for both approaches
Conclusion
Deleting a linked list in JavaScript is straightforward - simply set the head reference to null. JavaScript's garbage collector handles memory cleanup automatically, making the simple approach both efficient and safe.
