Delete nodes having greater value on right | Practice | GeeksforGeeks

Given a singly linked list, remove all the nodes which have a greater value on their right side.
Example 1:
Input:
LinkedList = 12->15->10->11->5->6->2->3
Output: 15 11 6 3
Explanation: Since, 12, 10, 5 and 2 are
the elements which have greater elements
on the following nodes. So, after deleting
them, the linked list would like be 15,
11, 6, 3.

Example 2:
Input:
LinkedList = 10->20->30->40->50->60
Output: 60

Your Task:
The task is to complete the function compute() which should modify the list as required and return the head of the modified linked list. The printing is done by the driver code,
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ size of linked list ≤ 1000
1 ≤ element of linked list ≤ 1000
Note: Try to solve the problem without using any extra space.
+1
mdzama15 hours ago
3 Line Recursive Code || C++ || Simple
Node* compute(Node *head) {
        if (!head || !head->next) return head;  // Base Case.
        Node* x = compute(head->next);
        (head->data >= x->data) ? head->next = x : head = x;
        return head;
    }
0
rahulparmar2210982 days ago
    Node *compute(Node *head)   {              stack<Node*> s;       Node *cur = head;              while(cur){                      while(!s.empty() && s.top()->data < cur->data){               s.pop();           }                      s.push(cur);                                 cur = cur->next;       }              Node *ans = NULL;              while(!s.empty()){            s.top()->next = ans;            ans =s.top();            s.pop();       }             return ans;          }
0
vikasrajpoot4794 days ago
/*c++ solution first reverse and then delete value small void reverse(Node*&head)   {       if(head==NULL || head->next==NULL)       {           return ;       }       Node*prev=NULL;       Node*curr=head;       Node*next=NULL;       while(curr!=NULL)       {           next=curr->next;           curr->next=prev;           prev=curr;           curr=next;       }       head=prev;   }   Node *compute(Node *head)   {       reverse(head);       Node*prev=head;       Node*curr=head->next;       while(curr!=NULL)       {           if(curr->data<prev->data)           {               prev->next=curr->next;               delete curr;           }           else{               prev=curr;           }           curr=curr->next;       }       reverse(head);       return head;   }er to newHead*/
 
+1
stksangal124 days ago
C++
WITHOUT REVERSE
WITHOUT RECURSION
SIMPLE EASY APPROACH
TIME TAKEN 0.23/1.6
Node *compute(Node *head)
    {
        // your code goes here
        Node* h=head;
        Node* n=h;
        while(h->next!=NULL){
            if(h->data < h->next->data){
                n=h;
                n->data=n->next->data;
                n->next=n->next->next;
                h=head;
            }
            else{
                h=h->next;
            }
        }
        return head;
    }
0
swapniltayal4225 days ago
void reverseLL(Node* &head){   Node* curr = head;   Node* prev = NULL;   Node* nxt;   while(curr != NULL){       nxt = curr->next;       curr->next = prev;       prev = curr;       curr = nxt;   }   head = prev;}
class Solution{   public:   Node *compute(Node *head)   {       reverseLL(head);       int max = -1;       Node* ans = new Node(-1);       Node* anstemp = ans;       Node* temp = head;       while(temp != NULL){           if (temp->data >= max){               max = temp->data;               Node* n = new Node(temp->data);               anstemp->next = n;               anstemp = anstemp->next;           }temp = temp->next;       }       ans = ans->next;       reverseLL(ans);       return ans;   }   };
+1
sidhumoosewala6 days ago
java solution :- with same logic as given in hint ;]
 
Node reverseLL(Node head)   {       Node prev = null, curr = head, ptr = null;              while(curr!=null)       {           ptr = curr.next;           curr.next = prev;           prev = curr;           curr = ptr;       }              return prev;   }      Node compute(Node head)   {       if(head==null || head.next==null) return head;       head = reverseLL(head);              Node temp = head;       int max = Integer.MIN_VALUE;       Node prev = null;              while(temp!=null)       {           if(temp.data>max) max=temp.data;           else if(temp.data<max)           {               prev.next = temp.next;               temp = temp.next;               continue;           }           prev = temp;           temp = temp.next;       }              head = reverseLL(head);              return head;   }
0
2019sushilkumarkori3 weeks ago
class Solution
{
    public:
    
    Node* reverse(Node* head){
        Node* curr = head;
        Node* prev = NULL;
        while(curr!=NULL){
            Node* nextptr = curr->next;
            curr->next = prev;
            prev = curr;
            curr = nextptr;
        }
        return prev;
    }
    Node *compute(Node *head)
    {
        // your code goes here
        head = reverse(head);
        int maxi = head->data;
        Node* prev = head;
        Node* curr = head->next;;
        while(curr!=NULL){
            if(curr->data < maxi){
                Node* Next = curr->next; 
                prev->next = Next;
                curr = Next;
            }
            else{
                prev = curr;
                maxi = curr->data;
                curr = curr->next;
            }
        }
        return reverse(head);
    }
    
};
0
mannukhurana103973 weeks ago
Java Recurssion
 
Node compute(Node head)
    {
        // time: 2*O(n) : 1.56/3.05
        // space O(0)
        if (head.next==null)
		    return head;
	    head.next = compute(head.next);
	    return head.data < head.next.data ? head.next : head;
    }
 
0
shakshamkaushik13 weeks ago
class Solution{   Node reverse(Node head){       Node curr = head;       Node prev = null;       while(curr!=null){           Node temp = curr.next;           curr.next = prev;           prev = curr;           curr = temp;       }       return prev;   }   Node compute(Node head)   {       Node rev = reverse (head);       int max = rev.data;       Node d = new Node(0);       Node res = d;       Node curr = rev;       while(curr!=null){           if(curr.data>=max){               max = curr.data;               d.next = new Node(curr.data);               d = d.next;           }           curr = curr.next;       }       return reverse (res.next);          }} 
+1
6288263 weeks ago
Beautiful recursive.
Node *compute(Node *head)
{
	if (! head || ! head->next)
		return head;
		
	head->next = compute(head->next);
		
	return head->data < head->next->data ? head->next : head;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.