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:

• 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.

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;
}
}
constructor() {
}
const node = new Node(value);
return;
}
while (current.next) {
current = current.next;
}
current.next = node;
}
// Function to delete nodes with greater value on right
deleteNodes() {
let prev = null;
// 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
}
}
}
// Test the code
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

159 Views