JavaScript Program To Delete Nodes Which Have A Greater Value On Right Side


We will be implementing a function to delete nodes in a linked list which have a greater value on their right side. The approach is to traverse the linked list from right to left and keep track of the maximum value encountered so far. For each node, we will compare its value with the maximum value and delete the node if its value is less than the maximum value. This way, all the nodes with values greater than the maximum value on their right side will be deleted.

Approach

The approach to delete nodes which have a greater value on their right side can be explained in 7 steps as follows:

  • Traverse the linked list from head to tail.

  • Keep track of the current node, previous node and the maximum value seen so far.

  • If the current node has a value less than the maximum value seen so far, delete the current node by updating the previous node's next pointer.

  • Update the maximum value seen so far to be the value of the current node.

  • Move the current node to the next node.

  • Repeat steps 3 to 5 until the end of the linked list is reached.

  • Return the head of the updated linked list.

Example

Given a singly linked list, the task is to delete nodes which have a greater value on right side. The idea is to traverse the list from right to left and keep track of the maximum value seen so far. When we traverse the list, we delete the node whose value is smaller than the maximum value seen so far.

Here is an implementation in JavaScript −

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}
class LinkedList {
  constructor() {
    this.head = null;
  }
  // Add a new node to the linked list
  add(value) {
    const node = new Node(value);
    if (!this.head) {
      this.head = node;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = node;
  }
  // Function to delete nodes with greater value on right
  deleteNodes() {
    let prev = null;
    let current = this.head;
    let max = this.head.value;
    // Traverse the linked list from right to left
    while (current.next) {
      // If the current node has a greater value than the max value seen so far
      if (current.next.value > max) {
        max = current.next.value;
        prev = current;
      } else {
        // Delete the node with smaller value
        prev.next = current.next;
      }
      current = current.next;
    }
    // If the last node has a smaller value than the max value seen so far
    if (this.head.value < max) {
      this.head = this.head.next;
    }
  }
}
// Test the code
const linkedList = new LinkedList();
linkedList.add(12);
linkedList.add(15);
linkedList.add(10);
linkedList.add(11);
linkedList.add(5);
linkedList.add(6);
linkedList.add(2);
linkedList.add(3);
linkedList.deleteNodes();
let current = linkedList.head;
while (current) {
  console.log(current.value);
  current = current.next;
}

Explanation

  • First, we create a linked list class with a Node class to define each node in the list.

  • In the LinkedList class, we have a function add() to add new nodes to the list.

  • The deleteNodes() function implements the logic to delete nodes with a greater value on the right side.

  • We traverse the list from right to left, keeping track of the maximum value seen so far.

  • If the current node has a greater value than the max value, we update the max value.

  • If the current node has a smaller value than the max value, we delete the node by updating the next reference of the previous node to point to the next node of the current node.

  • Finally, if the first node has a smaller value than the max value, we update the head reference to point to the next node of the first node.

  • The linked list after the nodes are deleted will only contain nodes with values that

Updated on: 13-Mar-2023

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements