Delete nodes which have a greater value on right side using recursion
Given a singly linked list, remove all the nodes which have a greater value on the right side.
Examples:
a) The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side.
When we examine 12, we see that after 12 there is one node with a value greater than 12 (i.e. 15), so we delete 12.
When we examine 15, we find no node after 15 that has a value greater than 15 so we keep this node.
When we go like this, we get 15->6->3
b) The list 10->20->30->40->50->60->NULL should be changed to 60->NULL. Note that 10, 20, 30, 40 and 50 have been deleted because they all have a greater value on the right side.
c) The list 60->50->40->30->20->10->NULL should not be changed.
Approach: We have already solved this problem by using 2 loops and reversing linked list in the post Delete nodes which have a greater value on right side
Here we will discuss the solution without reversing the list. We will use recursion to solve this problem in which the base case would be when the head is pointing to NULL. Else we would be recursively calling function for the next node and updating max value if currentNode->data > currentMax. In this way, the whole list would be updated.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int maxVal( int a, int b)
{
if (a > b)
return a;
return b;
}
struct Node* delNodes( struct Node* head, int * max)
{
if (head == NULL) {
return head;
}
head->next = delNodes(head->next, max);
if (head->data < *max) {
return head->next;
}
*max = maxVal(head->data, *max);
return head;
}
void push( struct Node** head, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = *head;
*head = new_node;
}
void printList( struct Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
cout << endl;
}
int main()
{
struct Node* head = NULL;
push(&head, 3);
push(&head, 2);
push(&head, 6);
push(&head, 5);
push(&head, 11);
push(&head, 10);
push(&head, 15);
push(&head, 12);
cout << "Given Linked List" << endl;
printList(head);
int max = INT_MIN;
head = delNodes(head, &max);
cout << "Modified Linked List" << endl;
printList(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node head;
static int max;
static int maxVal( int a, int b)
{
if (a > b)
return a;
return b;
}
static Node delNodes(Node head)
{
if (head == null )
{
return head;
}
head.next = delNodes(head.next);
if (head.data < max)
{
return head.next;
}
max = maxVal(head.data, max);
return head;
}
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
static void printList(Node head)
{
while (head != null )
{
System.out.print(head.data + " " );
head = head.next;
}
System.out.println();
}
public static void main(String[] args)
{
head = null ;
push(head, 3 );
push(head, 2 );
push(head, 6 );
push(head, 5 );
push(head, 11 );
push(head, 10 );
push(head, 15 );
push(head, 12 );
System.out.println( "Given Linked List" );
printList(head);
max = Integer.MIN_VALUE;
head = delNodes(head);
System.out.println( "Modified Linked List" );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data, next ):
self .data = data
self . next = next
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data, self .head)
self .head = new_node
def delNodes( self , head):
if head = = None :
return head
global Max
head. next = self .delNodes(head. next )
if head.data < Max :
return head. next
Max = max (head.data, Max )
return head
def printList( self ):
curr = self .head
while curr:
print (curr.data, end = " " )
curr = curr. next
print ()
if __name__ = = "__main__" :
linkedList = LinkedList()
linkedList.push( 3 )
linkedList.push( 2 )
linkedList.push( 6 )
linkedList.push( 5 )
linkedList.push( 11 )
linkedList.push( 10 )
linkedList.push( 15 )
linkedList.push( 12 )
print ( "Given Linked List" )
linkedList.printList()
Max = float ( '-inf' )
linkedList.head = linkedList.delNodes(linkedList.head)
print ( "Modified Linked List" )
linkedList.printList()
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node head;
static int max;
static int maxVal( int a, int b)
{
if (a > b)
return a;
return b;
}
static Node delNodes(Node head)
{
if (head == null )
{
return head;
}
head.next = delNodes(head.next);
if (head.data < max)
{
return head.next;
}
max = maxVal(head.data, max);
return head;
}
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
static void printList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
Console.WriteLine();
}
public static void Main(String[] args)
{
head = null ;
push(head, 3);
push(head, 2);
push(head, 6);
push(head, 5);
push(head, 11);
push(head, 10);
push(head, 15);
push(head, 12);
Console.WriteLine( "Given Linked List" );
printList(head);
max = int .MinValue;
head = delNodes(head);
Console.WriteLine( "Modified Linked List" );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var head;
var max;
function maxVal(a, b) {
if (a > b) return a;
return b;
}
function delNodes(head) {
if (head == null ) {
return head;
}
head.next = delNodes(head.next);
if (head.data < max) {
return head.next;
}
max = maxVal(head.data, max);
return head;
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
document.write( "<br>" );
}
head = null ;
push(head, 3);
push(head, 2);
push(head, 6);
push(head, 5);
push(head, 11);
push(head, 10);
push(head, 15);
push(head, 12);
document.write( "Given Linked List <br>" );
printList(head);
max = -2147483648;
head = delNodes(head);
document.write( "Modified Linked List <br>" );
printList(head);
</script>
|
Output:
Given Linked List
12 15 10 11 5 6 2 3
Modified Linked List
15 11 6 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
30 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...