Table of contents
1.
Introduction
2.
Brute-Force Approach
2.1.
PseudoCode
2.2.
CODE IN C++
2.3.
Complexity Analysis
3.
Approach(Efficient)
3.1.
CODE IN C++(Efficient)
3.2.
Complexity Analysis
4.
Frequently Asked Questions
4.1.
What is the time complexity of reversing a linked list? 
4.2.
What is the difference between a Singly Linked List and a Doubly Linked List?
4.3.
What is the Circular Linked List?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Delete Nodes that have a Greater Value on the Right Side in the Linked List

Author Ayush Tiwari
0 upvote
Linked List

Introduction

In this blog, we will discuss an important problem Delete all nodes that have a greater value on the right side in the linked list. Such problems do come in interviews. Before solving the problem, it’s recommended that a known basic traversal in the Linked List and how to reverse a Linked List.

In this blog, we dive deep into each detail to delete nodes that have a greater value on the right side.

Also see, Data Structures

Let’s look at the problem statement.

We will be given a head pointer of a singly linked list of size n, implementing a function to delete all the nodes which have a greater valued node on the right side.

E.g.: 

Illustration Image

As 20 has a greater value of 25 on its right and similarly, for 19, we have 25 on its right side so that these nodes will be deleted from the linked list.

So our result looks like:

Illustration Image

Example 1

Input-
List = 14->15->20->12->11->NULL
Output: 
List = 20->12->11->NULL

 

Explanation: 

As 14 has a greater value of 15 on its right and 15 has a greater value of 20 on its right, so this two-node was deleted.

Recommended Topic, Floyds Algorithm

Brute-Force Approach

First, let’s look at the naive approach that would probably strike anybody’s mind before jumping to an optimal approach.

So, the basic approach is to traverse the linked list from left to right, and for each node, check whether there is any node having a value greater than it or not on the right side. If yes, track of the delete that node. Else move to the next node.

Now let’s look at the PseudoCode.

PseudoCode

Algorithm

//Delete all nodes that have a greater value on the right side 
// pass list1,list2 and value target
countPairs(list)
Answer = list
  while list != NULL
      Data1 = list_value
      Temp = list_next
      while Temp != NULL
         Data2 = Temp_value
         if Data2 > Data1 
            Delete list
            Break loop
         Temp = Temp_next
      list = list_next

Return Answer 

CODE IN C++

//Delete all nodes that have a greater value on the right side
#include<bits/stdc++.h>
using namespace std;
// Linked list node
class Node{
public:
  int value;    //stores data of linked list
  Node *next;   //pointer for next node
  Node(int data){  //constructor to construct new node
      value=data;  // with given data 
      next=NULL;   
  }
};
/* function to Delete all nodes that have a greater value on the right side*/
void delete_nodes(Node *head){


  while(head != NULL){ // traverse the linked list
     int data1 = head -> value;  //select a node in first linked list 
     Node *temp = head;
     //boolean to check if there is node in right with greater value
     bool flag = false;
     while(temp != NULL){   // traverse on the right side
       int data2 = temp -> value; 
       if(data2 > data1){
         flag = true;
         break;
       }
       temp=temp->next; 
     }
     if( flag){ // it means I have to delete this node
       Node *dummy = head -> next;
       head -> value = head -> next -> value;
       head -> next = head -> next -> next;
       delete dummy;
     }
     else 
       head = head->next;
  }
// return resultant linkedlist.
  return head;
}
int main(){
// create first linked list with head pointer head1
  Node *head = new Node(20);
  head->next = new Node(19);
  head->next->next = new Node(25);
  head->next->next->next = new Node(23);
  head->next->next->next->next = new Node(16);
  delete_nodes(head); 
  // print the list :
  while(head){
    cout<<head ->value<<” ”;
    head = head->next;
  }
}
You can also try this code with Online C++ Compiler
Run Code

 

Input: 
head -> 20 -> 19 -> 25 -> 23 -> 16
Output: 
head -> 25 -> 23 -> 16

Complexity Analysis

Time Complexity: O(n2) where n = size of first linked list and m=size of second linked list.

This is because we iterate through two nested loops. Hence it’s O(n2).

Space complexity: O(1) at the worst case because no extra space is used.

The above algorithm works in O(n2) time which is pretty slow. This gives us the motivation to improve our algorithm.

So let’s think about an efficient approach to solve the problem.

Approach(Efficient)

This is one of the most efficient approaches to deleting all nodes that have a greater value on the right side. 

Now we can formulate our approach:

  1. First, we have to traverse the linked list from right to left to left, so we have to reverse the linked list and traverse through left to right.
  2. Now traverse the linked list and keep the track of maximum element so far.
  3. For every node, we have two possibilities either the node’s value is greater than or equal to the maximum value so far or less than the maximum value so far:
    1. If the node’s value is greater than or equal to the maximum value so far, then update our maximum and move to the next node.
    2. If the node’s value is less than the maximum value so far, delete that node and move to the next node.
  4. Reverse the linked list and return it.

Let’s look at the Code.

CODE IN C++(Efficient)

#include<bits/stdc++.h>
using namespace std;


// Linked list node
class Node{
public:
  int value;    //stores data of linked list
  Node *next;   //pointer for next node
  Node(int data){  //constructor to construct new node
      value=data;  // with given data 
      next=NULL;   
  }
};


// function to reverse the linked list;
Node* reverse_list(Node *head){
    Node *prev = NULL;
    Node *curr = head;
    Node *Next = NULL;
    while(curr != NULL){
       Next = curr -> next;
       curr -> next = prev;
       prev = curr;
       curr = Next;
    }
    return prev;
}
/* function to Delete all nodes that have a greater value on the right side*/
Node* delete_nodes(Node *head){
  // First I have to reverse the linked list
  Node *rev_head = reverse_list(head);
  head = rev_head;
  // traverse the linked list
  int max_so_far = INT_MIN;
  Node *prev = rev_head;
  while(rev_head != NULL){ 
    int data = rev_head -> value;
    if(data >= max_so_far){
       prev = rev_head;
       rev_head = rev_head -> next;
       max_so_far = data; 
    }
    else { // delete the current node
       Node *temp = rev_head;
      prev->next = rev_head -> next;
      rev_head = rev_head -> next;
      delete temp;
    }
  }
// reverse the result and return
 return reverse_list(head);
}


int main(){
// create first linked list with head pointer head1
  Node *head = new Node(20);
  head->next = new Node(19);
  head->next->next = new Node(25);
  head->next->next->next = new Node(23);
  head->next->next->next->next = new Node(16);


  head = delete_nodes(head); 
  // print the list :
  while(head){
    cout<<head ->value<<” ”;
    head = head->next;
  }

}
You can also try this code with Online C++ Compiler
Run Code

 

Input: 
head -> 20 -> 19 -> 25 -> 23 -> 16
Output: 
head -> 25 -> 23 -> 16

Complexity Analysis

Time Complexity: The time complexity is O(n) where n = the size of the linked list.

Space complexity: The Space Complexity is O(1) in the worst case because no extra space is used.

Frequently Asked Questions

What is the time complexity of reversing a linked list? 

Since we traverse a linked list once so time complexity is O(n) where n is the number of nodes in the linked list.

What is the difference between a Singly Linked List and a Doubly Linked List?

In Singly Linked List, each node has the address of the pointer to its next node, whereas, in Doubly Linked List, each node has the address of the pointers, its next node, and its previous node.

What is the Circular Linked List?

In Circular Linked List, the tail of the linked list points to the head of the same linked list.

Conclusion

This article taught us how to solve the problem, and delete all nodes that have a greater value on the right side We also saw how to approach the problem using a naive approach followed by efficient solutions. We discussed an iterative implementation and some basic traversal in linked lists using examples, pseudocode, and code in detail.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Cheers!

Live masterclass