Remove elements from singly linked list in JavaScript


In the given problem statement we are presented with a singly linked list and our task is to remove one element from the list. So we will discuss this operation step by step below.

What is a singly Linked List ?

A singly linked list consists of a series of nodes. It is a type of data structure. In this every node contains a value or we can say data and the list to the next node in the sequence. The first node in the list is called the head and the last node in the list points to null which indicates that this is the end of the list.

Below is an example visualization of a singly linked list:

In the above example the linked list contains nodes with two values. The first is the data part and second is the reference point to the next node. This is how the sequence is formed.

The main property of a singly linked list is that it allows for efficient insertion and deletion at the beginning of the list or after a given node. But accessing items in the middle of the list or searching for a value has a linear time complexity and requires traversing the list from the end.

These links can be useful when we frequently need to update or modify the list by adding or deleting elements at the beginning or end.

Understanding the problem

The problem is given a linked list which contains elements connected to every next value. We need to implement a function that will remove a specified element from the list. For example we have a list like 1 -> 2 -> 3 -> 4 -> 5, this is a linked list every element is connected to the its next element and we need to remove the element suppose we are removing 4 from the list so after removing the 4 linked list will look like 1 -> 2 -> 3 -> 5.

Logic for the given problem

We have given a singly linked list and we have to create a function which removes the specific item from the list. To solve this problem we will traverse the linked list. And start from the head of the linked list. After that we will move through the list one node at a time and do this task until we have not found the node to remove or reach the end of the list.

Then we will remove the specified item by checking the condition that the item to remove is found at the head of the list then update the head to the next node. Otherwise we will keep track of the current node and the previous node while traversing the list. If the node to remove is found so update the reference of the previoud node to skip the node to be removed. And return the updated linked list.

Algorithm

Step 1: As we have to remove the item from the given linked list so first we will create a class to represents node in the linked list. This class will have two properties like 'data', which is used to store the value of the node and 'next', which will point to the next node in the list.

Step 2: After creating node we will create the class for linked list itself. It will have a single property called head which will point to the first node in the list. If the list will be empty then head will be set to null.

Step 3: Now we will define a method to add the items in the node of the linked list. In this we will create a new node with the given data and append it tp the end of the list. Every new item will be added to the last item of the linked list.

Step 4: As we have to perform the given task to remove an item from the list. So create a function to remove the item with the specified data from the list. It will handle two cases first case is: if the node to remove is head then update the head reference to skip the current head. Otherwise we will travers the list while keeping the track of the current node and its previous node and continue doing this process until it will not find the node to remove. And we will update the previous node's next reference to skip the current node.

Step 5: Now our task is to display the linked list after removing the node. We will start from the head and append the data of each node to an array. And finally it will log the joined array items, separated by an arrow.

Example

//Node in the linked list
class Node {
   constructor(data) {
      this.data = data;
      this.next = null;
   }
}
// The linked list
class LinkedList {
   constructor() {
      this.head = null;
   }
   // Add items to the linked list
   add(data) {
      const newNode = new Node(data);

      if (!this.head) {
         this.head = newNode;
      } else {
         let current = this.head;
         while (current.next) {
            current = current.next;
         }
         current.next = newNode;
      }
   }
   // Remove item from the list  
   remove(data) {
      if (!this.head) {
         return;
      }

      if (this.head.data === data) {
         this.head = this.head.next;
         return;
      }

      let current = this.head;
      let previous = null;

      while (current && current.data !== data) {
         previous = current;
         current = current.next;
      }

      if (current) {
         previous.next = current.next;
      }
   }
   //Traverse and print the linked list
   display() {
      let current = this.head;
      let elements = [];

      while (current) {
         elements.push(current.data);
         current = current.next;
      }

      console.log(elements.join(" -> "));
   }
}

// Example
const list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);

console.log("Before removal:");
list.display();

list.remove(3);

console.log("After removal:");
list.display();

Output

Before removal:
1 -> 2 -> 3 -> 4 -> 5
After removal:
1 -> 2 -> 4 -> 5

Complexity

The time complexity to remove an item from the singly linked list is O(n) in which n is the number of nodes in the list. And in worst case this time may be increase because we will have to traverse the entire list to get the node to remove. The space complexity is O(1) as we are modifying the list in place without using any other storage.

Conclusion

In the given problem we have focused on removing the specific items from the singly linked list. By iterating the list and updating the references we can effectively remove the required item from the list with the time complexity of O(n).

Updated on: 16-Aug-2023

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements