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.

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

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

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>
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;
}
C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

// Function to delete nodes which have a
// greater value on the right side
struct Node *compute(struct Node *head) {

    if (head == NULL || head->next == NULL) {
        return head;
    }

    // FIX: call compute instead of old function name
    struct Node *nextNode = compute(head->next);

    if (nextNode->data > head->data) {
        free(node);
        return nextNode;
    }

    head->next = nextNode;
    return head;
}

void printList(struct Node *curr) {
    while (curr != NULL) {
        printf(" %d", curr->data);
        curr = curr->next;
    }
    printf("\n");
}

struct Node *createNode(int new_data) {
    struct Node *new_node =
      (struct Node *)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    return new_node;
}

int main() {

    // Create linked list:
    // 12 -> 15 -> 10 -> 11 -> 5 -> 6 -> 2 -> 3
    struct Node *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);

    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) {
        Data = data;
        Next = null;
    }
}

// This function deletes nodes on the 
// right side of the linked list
class GfG {
    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;
        }

        // find the next node using recursion
      	// It will return the node with the 
      	// greatest value on right side.
        Node nextNode = 
          compute(head.Next);

        // if right node's value is greater than
      	// current node's value, then we can simply 
      	// return the next node
        if (nextNode.Data > head.Data) {
            return nextNode;
        }

        // if current node's value is greater, then
      	// point it to the next node, and return the
      	// the current node.
        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() {
      
        // 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
// 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

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

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

Node* reverseList(Node* head);

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;
    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 curr
        else {
            curr = curr->next;
            maxnode = curr;
        }
    }

    // 3) Reverse the linked list again to 
    // retain the original order
    return reverseList(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;
    }
    return prev;
}

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;
    }
    cout << "\n";

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->next = NULL;
    return node;
}

struct Node* reverseList(struct Node* head);

struct Node* compute(struct 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.
    struct Node* curr = head;
    struct Node* maxnode = head;
    struct Node* 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 order
    return reverseList(head);
}

struct Node* reverseList(struct Node* headref) {
    struct Node* curr = headref;
    struct Node* prev = NULL;
    struct Node* next;
    while (curr!= NULL) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

int main() {
    
    // Create linked list
    // 12->15->10->11->5->6->2->3
    struct 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->next->next->next->next->next->next->next->next = NULL;

    head = compute(head);
    struct Node *curr = head;
    while (curr!= NULL) {
        printf(" %d", curr->data);
        curr = curr->next;
    }
    printf("\n");
    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