# 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 right side.

Examples:
a) The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->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 that after 12 there is one node with value greater than 12 (i.e. 15), so we delete 12.
When we examine 15, we find no node after 15 that has value greater than 15 so we keep this node.
When we go like this, we get 15->6->3

b) The list 10->20->30->40->50->60->NULL should be changed to 60->NULL. Note that 10, 20, 30, 40 and 50 have been deleted because they all have a greater value on the right side.

c) The list 60->50->40->30->20->10->NULL should not be changed.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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 exist 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 next node < 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 below code.

## C

```// C program to delete nodes which have a greater value on
// right side
#include <stdio.h>
#include <stdlib.h>

/* structure of a linked list node */
struct Node
{
int data;
struct Node *next;
};

/* prototype for utility functions */

/* Deletes nodes which have a node with greater value node
on left side */
{
/* 1) Reverse the linked list */

/* 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.*/

/* 3) Reverse the linked list again to retain the
original order */
}

/* Deletes nodes which have greater value node(s) on left side */
{

/* Initialize max */
struct Node *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;
free(temp);
}

/* If current is greater than max, then update max and
move current */
else
{
current = current->next;
maxnode = current;
}
}
}

/* Utility function to insert a node at the begining */
void push(struct Node **head_ref, int new_data)
{
struct Node *new_node =
(struct Node *)malloc(sizeof(struct Node));
new_node->data = new_data;
}

/* Utility function to reverse a linked list */
{
struct Node *prev = NULL;
struct Node *next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
}

/* Utility function to print a linked list */
{
{
}
printf("\n");
}

/* Driver program to test above functions */
int main()
{

12->15->10->11->5->6->2->3 */

return 0;
}```

## Java

```// Java program to delete nodes which have a greater value on
// right side
{

class Node
{
int data;
Node next;
Node(int d)  { data = d; next = null; }
}

/* Deletes nodes which have a node with greater
value node on left side */
void 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 */
void _delLesserNodes()
{

/* Initialise max */
Node 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 list. */
void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);

/* 3. Make next of new Node as head */

/* 4. Move the head to point to new Node */
}

/* Function to reverse the linked list */
void reverseList()
{
Node prev = null;
Node next;
while (current != null)
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
}

/* Function to print linked list */
void printList()
{
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}

/* Drier program to test above functions */
public static void main(String args[])
{

/* Constructed Linked List is 12->15->10->11->
5->6->2->3 */
llist.push(3);
llist.push(2);
llist.push(6);
llist.push(5);
llist.push(11);
llist.push(10);
llist.push(15);
llist.push(12);

llist.printList();

llist.delLesserNodes();

llist.printList();
}
} /* This code is contributed by Rajat Mishra */
```

Output:

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

Please write comments if you find the above code/algorithm incorrect, or find other ways to solve the same problem.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.5 Average Difficulty : 2.5/5.0
Based on 131 vote(s)