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.
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;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Function to delete nodes which have a// greater value on the right sidestructNode*compute(structNode*head){if(head==NULL||head->next==NULL){returnhead;}// FIX: call compute instead of old function namestructNode*nextNode=compute(head->next);if(nextNode->data>head->data){free(node);returnnextNode;}head->next=nextNode;returnhead;}voidprintList(structNode*curr){while(curr!=NULL){printf(" %d",curr->data);curr=curr->next;}printf("\n");}structNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;returnnew_node;}intmain(){// Create linked list:// 12 -> 15 -> 10 -> 11 -> 5 -> 6 -> 2 -> 3structNode*head=createNode(12);head->next=createNode(15);head->next->next=createNode(10);head->next->next->next=createNode(11);head->next->next->next->next=createNode(5);head->next->next->next->next->next=createNode(6);head->next->next->next->next->next->next=createNode(2);head->next->next->next->next->next->next->next=createNode(3);head=compute(head);printList(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){Data=data;Next=null;}}// This function deletes nodes on the // right side of the linked listclassGfG{staticNodecompute(Nodehead){// If next is NULL, then there is no node // with greater value on right side.if(head==null||head.Next==null){returnhead;}// find the next node using recursion// It will return the node with the // greatest value on right side.NodenextNode=compute(head.Next);// if right node's value is greater than// current node's value, then we can simply // return the next nodeif(nextNode.Data>head.Data){returnnextNode;}// if current node's value is greater, then// point it to the next node, and return the// the current node.head.Next=nextNode;returnhead;}staticvoidPrintList(Nodecurr){while(curr!=null){Console.Write(" "+curr.Data);curr=curr.Next;}Console.WriteLine();}staticvoidMain(){// 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
// 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
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.
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;}};Node*reverseList(Node*head);Node*compute(Node*head){// Reverse listhead=reverseList(head);// 2) In the reversed list, delete nodes // which have a node with greater value node // on the left side.Node*curr=head;Node*maxnode=head;Node*temp;while(curr!=nullptr&&curr->next!=nullptr){if(curr->next->data<maxnode->data){temp=curr->next;curr->next=temp->next;delete(temp);}// 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);}Node*reverseList(Node*headref){Node*curr=headref;Node*prev=nullptr;Node*next;while(curr!=nullptr){next=curr->next;curr->next=prev;prev=curr;curr=next;}returnprev;}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;}cout<<"\n";return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};structNode*newNode(intdata){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=data;node->next=NULL;returnnode;}structNode*reverseList(structNode*head);structNode*compute(structNode*head){// Reverse listhead=reverseList(head);// 2) In the reversed list, delete nodes // which have a node with greater value node // on the left side.structNode*curr=head;structNode*maxnode=head;structNode*temp;while(curr!=NULL&&curr->next!=NULL){if(curr->next->data<maxnode->data){temp=curr->next;curr->next=temp->next;free(temp);}else{curr=curr->next;maxnode=curr;}}// 3) Reverse the linked list again to // retain the original orderreturnreverseList(head);}structNode*reverseList(structNode*headref){structNode*curr=headref;structNode*prev=NULL;structNode*next;while(curr!=NULL){next=curr->next;curr->next=prev;prev=curr;curr=next;}returnprev;}intmain(){// Create linked list// 12->15->10->11->5->6->2->3structNode*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->next->next->next->next->next->next->next->next=NULL;head=compute(head);structNode*curr=head;while(curr!=NULL){printf(" %d",curr->data);curr=curr->next;}printf("\n");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);