C++ Program To Delete Nodes Which Have A Greater Value On Right Side
Given a singly linked list, remove all the nodes which have a greater value on the right side.
Examples:
Input: 12->15->10->11->5->6->2->3->NULL
Output: 15->11->6->3->NULL
Explanation: 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
Input: 10->20->30->40->50->60->NULL
Output: 60->NULL
Explanation: 10, 20, 30, 40, and 50 have been deleted because
they all have a greater value on the right side.
Input: 60->50->40->30->20->10->NULL
Output: No Change.
Method 1 (Simple):
Use two loops. In the outer loop, pick nodes of the linked list one by one. In the inner loop, check if there exists a node whose value is greater than the picked node. If there exists a node whose value is greater, then delete the picked node.
Time Complexity: O(n^2)
Method 2 (Use Reverse):
Thanks to Paras for providing the below algorithm.
1. Reverse the list.
2. Traverse the reversed list. Keep max till now. If the next node is less than max, then delete the next node, otherwise max = next node.
3. Reverse the list again to retain the original order.
Time Complexity: O(n)
Thanks to R.Srinivasan for providing the code below.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
void reverseList( struct Node** headref);
void _delLesserNodes( struct Node* head);
void delLesserNodes( struct Node** head_ref)
{
reverseList(head_ref);
_delLesserNodes(*head_ref);
reverseList(head_ref);
}
void _delLesserNodes( struct Node* head)
{
struct Node* current = head;
struct Node* maxnode = head;
struct Node* temp;
while (current != NULL &&
current->next != NULL)
{
if (current->next->data <
maxnode->data)
{
temp = current->next;
current->next = temp->next;
free (temp);
}
else
{
current = current->next;
maxnode = current;
}
}
}
void push( struct Node** head_ref,
int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
void reverseList( struct Node** headref)
{
struct Node* current = *headref;
struct Node* prev = NULL;
struct Node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*headref = prev;
}
void printList( struct Node* head)
{
while (head != NULL)
{
cout << " " << head->data ;
head = head->next;
}
cout << "" ;
}
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 " ;
printList(head);
delLesserNodes(&head);
cout << "Modified Linked List " ;
printList(head);
return 0;
}
|
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)
Source: https://www.geeksforgeeks.org/forum/topic/amazon-interview-question-for-software-engineerdeveloper-about-linked-lists-6
Please refer complete article on Delete nodes which have a greater value on right side for more details!
Last Updated :
30 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...