JavaScript Program to Remove Empty Node from Linked List
Last Updated :
15 Mar, 2024
Javascript allows us to remove the empty node from the linked list by traversing through the linked list and finding each empty node.
There are several approaches available in JavaScript through which this can be achieved which are as follows:
Iterative Approach
This approach traverses the linked list and checks if the node’s value is empty, null, undefined, or any other condition that represents an empty node. If the node is empty, remove it by updating the next pointer of the previous node to skip the empty node.
Example: Removing Null and Undefined Nodes from Linked List using iterative approach in JavaScript.
Javascript
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
function removeEmptyNodes(head) {
let current = head;
let previous = null;
while (current !== null) {
if (current.value === null
||
current.value === undefined) {
if (previous === null) {
head = current.next;
} else {
previous.next = current.next;
}
} else {
previous = current;
}
current = current.next;
}
return head;
}
function printList(head) {
while (head != null) {
console.log(head.value + " ");
head = head.next;
}
console.log("");
}
const node1 = new Node(10);
const node2 = new Node(null);
const node3 = new Node(20);
const node4 = new Node(undefined);
node1.next = node2;
node2.next = node3;
node3.next = node4;
console.log("Before Removing \n")
printList(node1);
const updatedHead = removeEmptyNodes(node1);
console.log("After Removing \n")
printList(updatedHead);
OutputBefore Removing
10
null
20
undefined
After Removing
10
20
Time Complexity: O(n), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant
Recursive Approach
This code defines a `ListNode` class to represent nodes in the linked list. The `removeEmptyNodes` function recursively removes empty nodes from the linked list. Finally, there’s a helper function `printList` to print the linked list for demonstration purposes.
Example: To demonstrate removing null nodes from a linked list in JavaScript using recursion.
Javascript
class ListNode {
constructor(val) {
this
.val = val;
this
.next = null;
}
}
function removeEmptyNodes(head) {
if (!head) {
return null;
}
head.next = removeEmptyNodes(head.next);
if (!head.val) {
return head.next;
} else {
return head;
}
}
let list = new ListNode(1);
list.next = new ListNode(2);
list.next
.next = new ListNode(null);
list.next
.next.next = new ListNode(3);
list.next.next
.next.next = new ListNode(null);
console.log("Original list:");
printList(list);
let result = removeEmptyNodes(list);
console.log("List after removing empty nodes:");
printList(result);
function printList(head) {
let current = head;
while (current !== null) {
console.log(current.val);
current = current.next;
}
}
OutputOriginal list:
1
2
null
3
null
List after removing empty nodes:
1
2
3
Time complexity: O(n)
Space complexity: O(n)
Share your thoughts in the comments
Please Login to comment...