Delete nodes which have a greater value on right side
Last Updated : 19 Oct, 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.
The key observation is that after removing all invalid nodes, the remaining nodes will be in decreasing order. We recursively process the nodes to the right and get the head of the modified list. This head node will contain the maximum value among the remaining nodes. Now, compare the current node with this value: if the current node is smaller, it has a greater value on its right and should be removed; otherwise, keep it.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Traverses the list in recursive manner Node*compute(Node*head){if(head==nullptr||head->next==nullptr){returnhead;}Node*nextNode=compute(head->next);if(nextNode->data>head->data){delete(head);returnnextNode;}head->next=nextNode;returnhead;}voidprintList(Node*curr){while(curr!=nullptr){cout<<" "<<curr->data;curr=curr->next;}}intmain(){Node*head=newNode(12);head->next=newNode(15);head->next->next=newNode(10);head->next->next->next=newNode(11);head->next->next->next->next=newNode(5);head->next->next->next->next->next=newNode(6);head->next->next->next->next->next->next=newNode(2);head->next->next->next->next->next->next->next=newNode(3);head=compute(head);// updated callprintList(head);return0;}
Java
classNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}publicclassGfG{// This function deletes nodes on the // right side of the linked liststaticNodecompute(Nodehead){// If next is NULL, then there is no node // with greater value on right side.if(head==null||head.next==null){returnhead;}// if right node's value is greater than// current node's value, then we can simply // return the next nodeNodenextNode=compute(head.next);// if current node's value is greater, then// point it to the next node, and return the// the current node.if(nextNode.data>head.data){returnnextNode;}// Else point the current node to next node // and return the head nodehead.next=nextNode;returnhead;}staticvoidprintList(Nodecurr){while(curr!=null){System.out.print(" "+curr.data);curr=curr.next;}}publicstaticvoidmain(String[]args){// Create linked list// 12->15->10->11->5->6->2->3Nodehead=newNode(12);head.next=newNode(15);head.next.next=newNode(10);head.next.next.next=newNode(11);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(2);head.next.next.next.next.next.next.next=newNode(3);head=compute(head);printList(head);}}
Python
# Python program to delete nodes # which have a greater value on# right sideclassNode:def__init__(self,data):self.data=dataself.next=None# This function deletes nodes on the # right side of the linked listdefcompute(node):# If next is NULL, then there is no node # with greater value on right side.ifnodeisNoneornode.nextisNone:returnnode# find the next node using recursion# It will return the node with the # greatest value on right side.next_node=compute(node.next)# if right node's value is greater than# current node's value, then we can simply # return the next nodeifnext_node.data>node.data:returnnext_node# if current node's value is greater, then# point it to the next node, and return the# the current node.node.next=next_nodereturnnodedefprint_list(curr):whilecurrisnotNone:print(f" {curr.data}",end="")curr=curr.nextprint()if__name__=="__main__":# Create a hard-coded linked list:# 12 -> 15 -> 10 -> 11 -> 5 -> 6 -> 2 -> 3head=Node(12)head.next=Node(15)head.next.next=Node(10)head.next.next.next=Node(11)head.next.next.next.next=Node(5)head.next.next.next.next.next=Node(6)head.next.next.next.next.next.next=Node(2)head.next.next.next.next.next.next.next=Node(3)head=compute(head)print_list(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;next=null;}}classGfG{staticNodecompute(Nodehead){// If next is NULL, there is no node on the right.if(head==null||head.next==null){returnhead;}// Recursively process the remaining list.NodenextNode=compute(head.next);// Remove the current node if a greater value exists on the right.if(nextNode.data>head.data){returnnextNode;}head.next=nextNode;returnhead;}staticvoidPrintList(Nodecurr){while(curr!=null){Console.Write(" "+curr.data);curr=curr.next;}Console.WriteLine();}staticvoidMain(){Nodehead=newNode(12);head.next=newNode(15);head.next.next=newNode(10);head.next.next.next=newNode(11);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(2);head.next.next.next.next.next.next.next=newNode(3);head=compute(head);PrintList(head);}}
JavaScript
// Node classclassNode{constructor(data){this.data=data;this.next=null;}}// Function to delete nodes having greater value on rightfunctioncompute(head){if(head===null||head.next===null){returnhead;}letnextNode=compute(head.next);if(nextNode.data>head.data){returnnextNode;}head.next=nextNode;returnhead;}// Print listfunctionprintList(curr){letres="";while(curr!==null){res+=curr.data+" ";curr=curr.next;}console.log(res.trim());}// Mainlethead=newNode(12);head.next=newNode(15);head.next.next=newNode(10);head.next.next.next=newNode(11);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(2);head.next.next.next.next.next.next.next=newNode(3);head=compute(head);printList(head);
Output
15 11 6 3
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.
Say the head= 8->3->13->2->5 , Follow below :
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function to reverse the linked listNode*reverseList(Node*head){Node*curr=head;Node*prev=nullptr;while(curr!=nullptr){Node*next=curr->next;curr->next=prev;prev=curr;curr=next;}returnprev;}Node*compute(Node*head){// Reverse the listhead=reverseList(head);// Remove nodes having a greater value on the leftNode*curr=head;Node*maxNode=head;while(curr!=nullptr&&curr->next!=nullptr){if(curr->next->data<maxNode->data){Node*temp=curr->next;curr->next=temp->next;deletetemp;}else{curr=curr->next;maxNode=curr;}}// Reverse again to retain original orderreturnreverseList(head);}intmain(){// Create linked list// 12 -> 15 -> 10 -> 11 -> 5 -> 6 -> 2 -> 3Node*head=newNode(12);head->next=newNode(15);head->next->next=newNode(10);head->next->next->next=newNode(11);head->next->next->next->next=newNode(5);head->next->next->next->next->next=newNode(6);head->next->next->next->next->next->next=newNode(2);head->next->next->next->next->next->next->next=newNode(3);head=compute(head);Node*curr=head;while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}return0;}
Java
importjava.util.*;classNode{publicintdata;publicNodenext;Node(intx){data=x;next=null;}}publicclassMain{staticNodereverseList(Nodehead){Nodecurr=head;Nodeprev=null;Nodenext;while(curr!=null){next=curr.next;curr.next=prev;prev=curr;curr=next;}returnprev;}staticNodecompute(Nodehead){// Reverse listhead=reverseList(head);// 2) In the reversed list, delete nodes // which have a node with greater value node // on the left side.Nodecurr=head;Nodemaxnode=head;while(curr!=null&&curr.next!=null){if(curr.next.data<maxnode.data){curr.next=curr.next.next;}// If curr is greater than max, // then update max and move currelse{curr=curr.next;maxnode=curr;}}// 3) Reverse the linked list again to // retain the original orderreturnreverseList(head);}publicstaticvoidmain(String[]args){// Create linked list// 12->15->10->11->5->6->2->3Nodehead=newNode(12);head.next=newNode(15);head.next.next=newNode(10);head.next.next.next=newNode(11);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(2);head.next.next.next.next.next.next.next=newNode(3);head=compute(head);Nodecurr=head;while(curr!=null){System.out.print(" "+curr.data);curr=curr.next;}System.out.println();}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefreverseList(head):curr=headprev=Nonenext=NonewhilecurrisnotNone:next=curr.nextcurr.next=prevprev=currcurr=nextreturnprevdefcompute(head):# Reverse Listhead=reverseList(head)# 2) In the reversed list, delete nodes #/ which have a node with greater value node # on the left side. curr=headmaxnode=headwhilecurrisnotNoneandcurr.nextisnotNone:ifcurr.next.data<maxnode.data:curr.next=curr.next.nextelse:curr=curr.nextmaxnode=curr# Reverse List returnreverseList(head)defprintList(head):curr=headwhilecurrisnotNone:print(''+str(curr.data),end='')curr=curr.nextprint()# Create linked list# 12->15->10->11->5->6->2->3head=Node(12)head.next=Node(15)head.next.next=Node(10)head.next.next.next=Node(11)head.next.next.next.next=Node(5)head.next.next.next.next.next=Node(6)head.next.next.next.next.next.next=Node(2)head.next.next.next.next.next.next.next=Node(3)head=compute(head)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classProgram{staticNodereverseList(Nodehead){Nodecurr=head;Nodeprev=null;Nodenext=null;while(curr!=null){next=curr.next;curr.next=prev;prev=curr;curr=next;}returnprev;}staticNodecompute(Nodehead){// Revese Listhead=reverseList(head);// 2) In the reversed list, delete nodes // which have a node with greater value node // on the left side.Nodecurr=head;Nodemaxnode=head;while(curr!=null&&curr.next!=null){if(curr.next.data<maxnode.data){curr.next=curr.next.next;}else{curr=curr.next;maxnode=curr;}}// Revese List returnreverseList(head);}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(" "+curr.data);curr=curr.next;}Console.WriteLine();}staticvoidMain(string[]args){// Create linked list// 12->15->10->11->5->6->2->3Nodehead=newNode(12);head.next=newNode(15);head.next.next=newNode(10);head.next.next.next=newNode(11);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(2);head.next.next.next.next.next.next.next=newNode(3);head=compute(head);printList(head);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}functionreverseList(head){letcurr=head;letprev=null;letnext=null;while(curr!==null){next=curr.next;curr.next=prev;prev=curr;curr=next;}returnprev;}functioncompute(head){// Reverse Listhead=reverseList(head);// 2) In the reversed list, delete nodes // which have a node with greater value node // on the left side. letcurr=head;letmaxnode=head;while(curr!==null&&curr.next!==null){if(curr.next.data<maxnode.data){curr.next=curr.next.next;}else{curr=curr.next;maxnode=curr;}}// Reverse ListreturnreverseList(head);}functionprintList(head){letcurr=head;while(curr!==null){process.stdout.write(''+curr.data);curr=curr.next;}console.log();}// Create linked list// 12->15->10->11->5->6->2->3lethead=newNode(12);head.next=newNode(15);head.next.next=newNode(10);head.next.next.next=newNode(11);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(2);head.next.next.next.next.next.next.next=newNode(3);head=compute(head);printList(head);