You are given the head of a linked list.
Remove every node which has a node with a strictly greater value anywhere to the right side of it.
Return the head of the modified linked list.
I have tried the method of recursion using the following code but nevertheless time limit exceeded is what I get
class Solution {
public:
ListNode* solve(ListNode* head)
{
ListNode* temp;
if(head->next=NULL)
return head;
if(head->next->val>head->val)
{
head=head->next;
}
for(temp=head;temp->next!=NULL;temp=temp->next)
{
while(temp->next->next!=NULL)
{
if(temp->next->val<temp->next->next->val)
temp->next=temp->next->next;
}
}
solve(head);
return head;
}
ListNode* removeNodes(ListNode* head) {
return solve(head);
}
};