JavaScript Program to Remove Empty Node from Linked List Last Updated : 15 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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: Table of Content Iterative ApproachRecursive ApproachIterative ApproachThis 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 ApproachThis 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) Comment More infoAdvertise with us Next Article JavaScript Program to Remove Empty Node from Linked List N nikitamehrotra99 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Remove Loop in a Linked List A linked list is a collection of nodes that contain both a data element and a reference (or pointer) to the next node in the collection. A common concern with linked lists is having loops and cycles. When one of the nodes in the linked list points towards any of its previous nodes leading to an infi 7 min read Delete Operations in Doubly Linked List using JavaScript This article will demonstrate how to perform delete operations in a Doubly Linked List Data Structure using JavaScript. We will see different approaches to deleting elements from the Doubly Linked List. Steps to Delete Element Node from Doubly Linked ListA doubly linked list has two pointers that li 11 min read Javascript Program For Removing Every K-th Node Of The Linked List Given a singly linked list, Your task is to remove every K-th node of the linked list. Assume that K is always less than or equal to length of Linked List.Examples :Input: 1->2->3->4->5->6->7->8 k = 3Output: 1->2->4->5->7->8As 3 is the k-th node after its deletion 3 min read Javascript Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5If there are even nodes, then there would be two middle nodes, we need to delete the second middle elemen 3 min read Javascript Program For Removing Duplicates From An Unsorted Linked List Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 -> 5 min read Javascript Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3.Method 4 min read Javascript Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and InsertionWrite a function to delete a given node in a doubly-linked list. Original Doubly Linked List Approach: The deletion of a node in a doubly-linked list can be divided into three main categories: After the deletion of the head node. After 4 min read JavaScript Linked List Programs JavaScript Linked List Programs contain a list of articles based on programming. Linked List is a linear data structure that stores data in linearly connected nodes. Linked lists store elements sequentially, but doesnât store the elements contiguously like an array. S. NoArticles1JavaScript Program 5 min read Javascript Program For Removing Middle Points From a Linked List Of Line Segments Given a linked list of coordinates where adjacent points either form a vertical line or a horizontal line. Delete points from the linked list which are in the middle of a horizontal or vertical line.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked 4 min read Javascript Program To Delete N Nodes After M Nodes Of A Linked List Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.Difficulty Level: Rookie Examples:Input:M = 2, N = 2Linked List: 1->2->3->4->5->6->7->8Output:Linked List: 3 min read Like