Delete nodes which have a greater value on right side
Last Updated : 10 May, 2026
Given a singly linked list, the task is to remove all the nodes with any node on their right whose value is greater and return the head of the modified linked list.
Examples:
Input: head: 12->15->10->11->5->6->2->3 Output: 15->11->6->3 Explanation: Node with value 12 , 10, 5, and 2 will be deleted as the greater value is present on right side of nodes.
Input: head: 10->20->30->40->50->60 Output: 60 Explanation: Node with value 10 , 20, 30, 40 and 50 will be deleted as the greater value is present on right side of nodes.
If we take a closer look, we can notice that the result list would always be having node in decreasing order. So the idea is to recursively call for the next of the first node. When the recursive function returns head of the remaining modified list, it would have the largest value in the remaining list and we only need to compare the first node with this.
Output
15 11 6 3
By Reversing the list - O(n) Time and O(1) Space
The idea is to reverse the linked list and maintain the maximum value from left side. If value of current node is greater than maximum value, then update the max value and move to next node. Otherwise, delete the current node. Reverse the resultant list and return it.
Step-by-step implementation:
Reverse the list so that we can easily maintain the maximum value from the left side.
Initialize a pointer maxnode which points to the node with maximum value on left side (initially set to head).
Traverse the list from head node. For each node, if its value is less than maxnode, then delete it. Otherwise, update the maxnode to current node.
Reverse the list again to retain the original order.