Open In App

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

Last Updated : 30 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a singly linked list, remove all the nodes which have a greater value on the right side. 

Examples:  

Input: 12->15->10->11->5->6->2->3->NULL
Output: 15->11->6->3->NULL
Explanation: 12, 10, 5 and 2 have been deleted because there is a 
             greater value on the right side. When we examine 12, 
             we see that after 12 there is one node with a value 
             greater than 12 (i.e. 15), so we delete 12. When we 
             examine 15, we find no node after 15 that has a value 
             greater than 15, so we keep this node. When we go like 
             this, we get 15->6->3

Input: 10->20->30->40->50->60->NULL
Output: 60->NULL
Explanation: 10, 20, 30, 40, and 50 have been deleted because 
             they all have a greater value on the right side.

Input: 60->50->40->30->20->10->NULL
Output: No Change.

Method 1 (Simple): 
Use two loops. In the outer loop, pick nodes of the linked list one by one. In the inner loop, check if there exists a node whose value is greater than the picked node. If there exists a node whose value is greater, then delete the picked node. 
Time Complexity: O(n^2)

Method 2 (Use Reverse): 
Thanks to Paras for providing the below algorithm. 
1. Reverse the list. 
2. Traverse the reversed list. Keep max till now. If the next node is less than max, then delete the next node, otherwise max = next node. 
3. Reverse the list again to retain the original order. 
Time Complexity: O(n)
Thanks to R.Srinivasan for providing the code below. 

Javascript




<script>
// Javascript program to delete nodes which
// have a greater value on right side
     
// head of list
var head;
 
// Linked list Node
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}
 
/* Deletes nodes which have a node
   with greater value node on left side */
function delLesserNodes()
{
    // 1.Reverse the linked list
    reverseList();
 
    /* 2. In the reversed list, delete nodes
          which have a node with greater value
          node on left side. Note that head node
          is never deleted because it is the leftmost
          node. */
    _delLesserNodes();
 
    /* 3. Reverse the linked list again to retain
          the original order */
    reverseList();
}
 
/* Deletes nodes which have greater value node(s)
   on left side */
function _delLesserNodes()
{
    var current = head;
 
    // Initialise max
    var maxnode = head;
    var temp;
 
    while (current != null &&
           current.next != null)
    {
        /* If current is smaller than max,
           then delete current */
        if (current.next.data < maxnode.data)
        {
            temp = current.next;
            current.next = temp.next;
            temp = null;
        }
 
        /* If current is greater than max,
           then update max and move current */
        else
        {
            current = current.next;
            maxnode = current;
        }
    }
}
 
// Utility functions
// Inserts a new Node at front of the 
function push(new_data)
{
    /* 1 & 2: Allocate the Node &
              Put in the data */
    var new_node = new Node(new_data);
 
    // 3. Make next of new Node as head
    new_node.next = head;
 
    // 4. Move the head to point to
    // new Node
    head = new_node;
}
 
// Function to reverse the linked list
function reverseList()
{
    var current = head;
    var prev = null;
    var next;
    while (current != null)
    {
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }
    head = prev;
}
 
// Function to print linked list
function printList()
{
    var temp = head;
    while (temp != null)
    {
        document.write(temp.data + " ");
        temp = temp.next;
    }
    document.write();
}
 
// Driver code
/* Constructed Linked List is
   12->15->10->11-> 5->6->2->3 */
push(3);
push(2);
push(6);
push(5);
push(11);
push(10);
push(15);
push(12);
 
document.write(
"Given Linked List<br/>");
printList();
 
delLesserNodes();
 
document.write(
"<br/>Modified Linked List<br/>");
printList();
// This code is contributed by aashish1995
</script>


Output:

Given Linked List 
 12 15 10 11 5 6 2 3
Modified Linked List 
 15 11 6 3

Time Complexity: O(n)

Auxiliary Space: O(1)

Method 3: 
The other simpler method is to traverse the list from the start and delete the node when the current Node < next Node. To delete the current node, follow this approach.Let us assume you have to delete current node X: 

  1. Copy next node’s data into X i.e X.data = X.next.data.
  2. Copy next node’s next address i.e X.next = X.next.next.

Move forward in the List only when the current Node is > the next Node.

Javascript




<script>
// Javascript program for above approach
// This class represents a single node
// in a linked list
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}
 
// This is a utility class for linked list
// This function creates a linked list from a
// given array and returns head
function createLL(arr)
{
    var head = new Node(arr[0]);
    var temp = head;
 
    var newNode = null;
    for (i = 1; i < arr.length; i++)
    {
        newNode = new Node(arr[i]);
        temp.next = newNode;
        temp = temp.next;
    }
    return head;
}
 
// This function prints given linked list
function printLL(head)
{
    while (head != null)
    {
        document.write(head.data +
                       " ");
        head = head.next;
    }
    document.write("<br/>");
}
 
// Main function
function deleteNodesOnRightSide(head)
{
    if (head == null ||
        head.next == null)
        return head;
    var nextNode =
        deleteNodesOnRightSide(head.next);
 
     if (nextNode.data > head.data)
         return nextNode;
 
     head.next = nextNode;
     return head;
}
var arr = [12, 15, 10, 11, 5, 6, 2, 3];
var head = createLL(arr);
document.write(
"Given Linked List<br/>");
printLL(head);
head = deleteNodesOnRightSide(head);
document.write(
"<br/>Modified Linked List<br/>");
printLL(head);
// This code is contributed by aashish1995
</script>


Output:

Given Linked List
12 15 10 11 5 6 2 3 
Modified Linked List
15 11 6 3

Time Complexity: O(n)

Auxiliary Space: O(1)

Source: https://www.geeksforgeeks.org/forum/topic/amazon-interview-question-for-software-engineerdeveloper-about-linked-lists-6

Please refer complete article on Delete nodes which have a greater value on right side for more details!



Previous Article
Next Article

Similar Reads

C++ Program To Delete Nodes Which Have A Greater Value On Right Side
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: Input: 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL Output: 15-&gt;11-&gt;6-&gt;3-&gt;NULL Explanation: 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see that after 12 th
4 min read
C Program To Delete Nodes Which Have A Greater Value On Right Side
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: Input: 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL Output: 15-&gt;11-&gt;6-&gt;3-&gt;NULL Explanation: 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see that after 12 th
4 min read
Java Program To Delete Nodes Which Have A Greater Value On Right Side
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: Input: 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL Output: 15-&gt;11-&gt;6-&gt;3-&gt;NULL Explanation: 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see that after 12 th
6 min read
Delete nodes which have a greater value on right side using recursion
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: a) The list 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL should be changed to 15-&gt;11-&gt;6-&gt;3-&gt;NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see
7 min read
Delete nodes which have a greater value on right side
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: a) The list 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL should be changed to 15-&gt;11-&gt;6-&gt;3-&gt;NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see
19 min read
Delete linked list nodes which have a greater value on left side
Given a singly linked list, the task is to remove all the nodes which have a greater value on the left side. Examples: Input: 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3Output: Modified Linked List = 12 15 Input: 25-&gt;15-&gt;6-&gt;48-&gt;12-&gt;5-&gt;16-&gt;14Output: Modified Linked List = 14 16 48 Approach: Initialize the maximum with head n
7 min read
Delete linked list nodes which have a Lesser Value on Left Side
Given a singly linked list, the task is to remove all the nodes which have a lesser value on left side. Examples: Input: 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3Output: Modified Linked List = 12 -&gt; 10 -&gt; 5 -&gt; 2 Input: 25-&gt;15-&gt;6-&gt;48-&gt;12-&gt;5-&gt;16-&gt;14Output: Modified Linked List = 25 -&gt; 15 -&gt; 6 -&gt; 5 Approach
7 min read
Count smaller elements on right side and greater elements on left side using Binary Index Tree
Given an array arr[] of size N. The task is to find smaller elements on the right side and greater elements on the left side for each element arr[i] in the given array. Examples: Input: arr[] = {12, 1, 2, 3, 0, 11, 4} Output: Smaller right: 6 1 1 1 0 1 0 Greater left: 0 1 1 1 4 1 2 Input: arr[] = {5, 4, 3, 2, 1} Output: Smaller right: 4 3 2 1 0 Gre
16 min read
Server Side Rendering vs Client Side Rendering vs Server Side Generation
In the world of web development, there are several approaches to rendering web pages: server-side rendering, client-side rendering, and server-side generation. Each approach has its own advantages and disadvantages, and choosing the right one for your project depends on your specific needs and goals. In this blog, we’ll explore the differences betw
4 min read
Count the number of elements which are greater than any of element on right side of an array
Given an array Arr[]. The task is to count the number of elements Arr[i] in the given array such that one or more smaller elements are present on the right side of the element Arr[i] in array. Examples: Input: Arr[] = { 3, 9, 4, 6, 7, 5 } Output: 3Numbers that counts are: 9, 6, 7 9 - As all numbers are present after 9 are smaller than 9, 6 - 5 is s
4 min read
Practice Tags :