Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program To Delete Nodes Which Have A Greater Value On Right Side
We will implement 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 compare its value with the maximum value and delete the node if its value is less than the maximum value.
Problem Understanding
Given a singly linked list, we need to delete all nodes that have at least one node with a greater value to their right. For example, in the list [12, 15, 10, 11, 5, 6, 2, 3], node 12 should be deleted because 15 is greater and appears on its right side.
Approach
The approach to delete nodes which have a greater value on their right side follows these steps:
Traverse the linked list from head to tail
Keep track of the current node, previous node and the maximum value seen so far from the right
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 if the current node's value is greater
Move to the next node and repeat until the end of the list is reached
Return the head of the updated linked list
Implementation
Here's the JavaScript implementation using a reverse traversal approach:
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() {
if (!this.head) return;
// Reverse the linked list
this.head = this.reverse(this.head);
// Delete nodes with smaller values than max seen so far
let maxSoFar = this.head.value;
let current = this.head;
while (current.next) {
if (current.next.value
Original list: [12, 15, 10, 11, 5, 6, 2, 3]
After deletion: [15, 11, 6, 3]
How It Works
The algorithm works by:
Reversing the list: We first reverse the linked list so we can traverse from right to left effectively
Tracking maximum: We keep track of the maximum value seen so far while traversing
Deleting smaller nodes: Any node with a value smaller than the current maximum is deleted
Restoring order: Finally, we reverse the list again to restore the original order
Example Walkthrough
For the input [12, 15, 10, 11, 5, 6, 2, 3]:
Node 12 is deleted because 15 > 12
Node 15 remains because it's the largest in its right side
Node 10 is deleted because 11 > 10
Node 11 remains because it's larger than all nodes to its right
Node 5 is deleted because 6 > 5
Node 6 remains because it's larger than nodes to its right
Node 2 is deleted because 3 > 2
Node 3 remains as it's the last node
Conclusion
This algorithm efficiently solves the problem in O(n) time complexity by using a reverse traversal approach. The key insight is to process nodes from right to left to maintain the maximum value seen so far, making deletion decisions straightforward.
