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.

Delete-nodes-which-have-a-greater-value-on-right-side

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.

Try It Yourself
redirect icon

Recursion - O(n) Time and O(n) Space

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>
using namespace std;

class Node {
public:
    int data;
    Node* next;
    
    Node(int x) {
        data = x;
        next = nullptr;
    }
};

// Traverses the list in recursive manner 
Node* compute(Node* head) {
  
    if(head == nullptr || head->next == nullptr) {
        return head;
    }
    
    Node* nextNode = compute(head->next);
    
    if (nextNode->data > head->data) {
        delete(head);
        return nextNode;
    }
    
    head->next = nextNode;
    
    return head;
}

void printList(Node* curr) {
    while (curr != nullptr) {
        cout << " " << curr->data;
        curr = curr->next;
    }
}

int main() {
  
    Node* head = new Node(12); 
    head->next = new Node(15);
    head->next->next = new Node(10);
    head->next->next->next = new Node(11);
    head->next->next->next->next = new Node(5);
    head->next->next->next->next->next = new Node(6);
    head->next->next->next->next->next->next = new Node(2);
    head->next->next->next->next->next->next->next = new Node(3);

    head = compute(head);  // updated call

    printList(head);

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class GfG {
  
    // This function deletes nodes on the 
  	// right side of the linked list
    static Node compute(Node head) {
      
        // If next is NULL, then there is no node 
      	// with greater value on right side.
        if (head == null || head.next == null) {
            return head;
        }

         // if right node's value is greater than
      	// current node's value, then we can simply 
      	// return the next node
        Node nextNode = 
          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) {
            return nextNode;
        }

        // Else point the current node to next node 
      	// and return the head node
        head.next = nextNode;
        return head;
    }

    static void printList(Node curr) {
        while (curr != null) {
            System.out.print(" " + curr.data);
            curr = curr.next;
        }
    }

    public static void main(String[] args) {
      
        // Create linked list
        // 12->15->10->11->5->6->2->3
        Node head = new Node(12);
        head.next = new Node(15);
        head.next.next = new Node(10);
        head.next.next.next = new Node(11);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);
        head.next.next.next.next.next.next = new Node(2);
        head.next.next.next.next.next.next.next = new Node(3);

        head = compute(head);

        printList(head);
    }
}
Python
# Python program to delete nodes 
# which have a greater value on
# right side

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

# This function deletes nodes on the 
# right side of the linked list
def compute(node):
    
    # If next is NULL, then there is no node 
    # with greater value on right side.
    if node is None or node.next is None:
        return node
    
    # 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 node
    if next_node.data > node.data:
        return next_node
    
    # if current node's value is greater, then
    # point it to the next node, and return the
  	# the current node.
    node.next = next_node
    return node

def print_list(curr):
    while curr is not None:
        print(f" {curr.data}", end="")
        curr = curr.next
    print()

if __name__ == "__main__":
    
    # Create a hard-coded linked list:
    # 12 -> 15 -> 10 -> 11 -> 5 -> 6 -> 2 -> 3
    head = 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#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int data) {
        this.data = data;
        next = null;
    }
}

class GfG {
    static Node compute(Node head) {

        // If next is NULL, there is no node on the right.
        if (head == null || head.next == null) {
            return head;
        }

        // Recursively process the remaining list.
        Node nextNode = compute(head.next);

        // Remove the current node if a greater value exists on the right.
        if (nextNode.data > head.data) {
            return nextNode;
        }

        head.next = nextNode;
        return head;
    }

    static void PrintList(Node curr) {
        while (curr != null) {
            Console.Write(" " + curr.data);
            curr = curr.next;
        }
        Console.WriteLine();
    }

    static void Main() {
        Node head = new Node(12);
        head.next = new Node(15);
        head.next.next = new Node(10);
        head.next.next.next = new Node(11);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);
        head.next.next.next.next.next.next = new Node(2);
        head.next.next.next.next.next.next.next = new Node(3);

        head = compute(head);
        PrintList(head);
    }
}
JavaScript
// Node class
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

// Function to delete nodes having greater value on right
function compute(head) {

    if (head === null || head.next === null) {
        return head;
    }

    let nextNode = compute(head.next);

    if (nextNode.data > head.data) {
        return nextNode;
    }

    head.next = nextNode;
    return head;
}

// Print list
function printList(curr) {
    let res = "";
    while (curr !== null) {
        res += curr.data + " ";
        curr = curr.next;
    }
    console.log(res.trim());
}

// Main
let head = new Node(12);
head.next = new Node(15);
head.next.next = new Node(10);
head.next.next.next = new Node(11);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.next = new Node(2);
head.next.next.next.next.next.next.next = new Node(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>
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

// Function to reverse the linked list
Node* reverseList(Node* head) {
    Node* curr = head;
    Node* prev = nullptr;

    while (curr != nullptr) {
        Node* next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }

    return prev;
}

Node* compute(Node* head) {

    // Reverse the list
    head = reverseList(head);

    // Remove nodes having a greater value on the left
    Node* 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;
            delete temp;
        }
        else {
            curr = curr->next;
            maxNode = curr;
        }
    }

    // Reverse again to retain original order
    return reverseList(head);
}

int main() {
    // Create linked list
    // 12 -> 15 -> 10 -> 11 -> 5 -> 6 -> 2 -> 3

    Node* head = new Node(12);
    head->next = new Node(15);
    head->next->next = new Node(10);
    head->next->next->next = new Node(11);
    head->next->next->next->next = new Node(5);
    head->next->next->next->next->next = new Node(6);
    head->next->next->next->next->next->next = new Node(2);
    head->next->next->next->next->next->next->next = new Node(3);

    head = compute(head);

    Node* curr = head;

    while (curr != nullptr) {
        cout << curr->data << " ";
        curr = curr->next;
    }

    return 0;
}
Java
import java.util.*;

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

public class Main {
    static Node reverseList(Node head) {
        Node curr = head;
        Node prev = null;
        Node next;
        while (curr!= null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

    static Node compute(Node head) {
        
        // Reverse list
        head = 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;
        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 curr
            else {
                curr = curr.next;
                maxnode = curr;
            }
        }
        
        // 3) Reverse the linked list again to 
        // retain the original order
        return reverseList(head);
    }

    public static void main(String[] args) {
        // Create linked list
        // 12->15->10->11->5->6->2->3
        Node head = new Node(12);
        head.next = new Node(15);
        head.next.next = new Node(10);
        head.next.next.next = new Node(11);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);
        head.next.next.next.next.next.next = new Node(2);
        head.next.next.next.next.next.next.next = new Node(3);

        head = compute(head);
        Node curr = head;
        while (curr!= null) {
            System.out.print(" " + curr.data);
            curr = curr.next;
        }
        System.out.println();
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None

def reverseList(head):
    curr = head
    prev = None
    next = None
    while curr is not None:
        next = curr.next
        curr.next = prev
        prev = curr
        curr = next
    return prev

def compute(head):
    
    # Reverse List
    head = reverseList(head)
    
    # 2) In the reversed list, delete nodes 
    #/ which have a node with greater value node 
    # on the left side.    
    curr = head
    maxnode = head
    while curr is not None and curr.next is not None:
        if curr.next.data < maxnode.data:
            curr.next = curr.next.next
        else:
            curr = curr.next
            maxnode = curr
            
    # Reverse List        
    return reverseList(head)

def printList(head):
    curr = head
    while curr is not None:
        print(''+ str(curr.data), end='')
        curr = curr.next
    print()

# Create linked list
# 12->15->10->11->5->6->2->3
head = 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#
using System;

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

class Program {
    static Node reverseList(Node head) {
        Node curr = head;
        Node prev = null;
        Node next = null;
        while (curr!= null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

    static Node compute(Node head) {
        
        // Revese List
        head = 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;
        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        
        return reverseList(head);
    }

    static void printList(Node head) {
        Node curr = head;
        while (curr!= null) {
            Console.Write(" " + curr.data);
            curr = curr.next;
        }
        Console.WriteLine();
    }

    static void Main(string[] args) {
        // Create linked list
        // 12->15->10->11->5->6->2->3
        Node head = new Node(12);
        head.next = new Node(15);
        head.next.next = new Node(10);
        head.next.next.next = new Node(11);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);
        head.next.next.next.next.next.next = new Node(2);
        head.next.next.next.next.next.next.next = new Node(3);

        head = compute(head);
        printList(head);
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

function reverseList(head) {
    let curr = head;
    let prev = null;
    let next = null;
    while (curr!== null) {
        next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

function compute(head) {
    
    // Reverse List
    head = reverseList(head);

    // 2) In the reversed list, delete nodes 
    // which have a node with greater value node 
    // on the left side.    
    let curr = head;
    let maxnode = 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 List
    return reverseList(head);
}

function printList(head) {
    let curr = head;
    while (curr!== null) {
        process.stdout.write(''+ curr.data);
        curr = curr.next;
    }
    console.log();
}

// Create linked list
// 12->15->10->11->5->6->2->3
let head = new Node(12);
head.next = new Node(15);
head.next.next = new Node(10);
head.next.next.next = new Node(11);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.next = new Node(2);
head.next.next.next.next.next.next.next = new Node(3);

head = compute(head);
printList(head);

Output
 15 11 6 3
Comment