C++ Program To Delete Nodes Which Have A Greater Value On Right Side
Last Updated :
30 Mar, 2022
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++
// C++ program to delete nodes which
// have a greater value on right side
#include <bits/stdc++.h>
using namespace std;
// Structure of a linked list node
struct Node
{
int data;
struct Node* next;
};
// Prototype for utility functions
void reverseList(struct Node** headref);
void _delLesserNodes(struct Node* head);
/* Deletes nodes which have a node with
greater value node on left side */
void delLesserNodes(struct Node** head_ref)
{
// 1. Reverse the linked list
reverseList(head_ref);
/* 2. In the reversed list, delete nodes
which have a node with greater value
node on left side. Note that head node
is never deleted because it is the
leftmost node.*/
_delLesserNodes(*head_ref);
/* 3. Reverse the linked list again to
retain the original order */
reverseList(head_ref);
}
/* Deletes nodes which have
greater value node(s) on left side */
void _delLesserNodes(struct Node* head)
{
struct Node* current = head;
// Initialize max
struct Node* maxnode = head;
struct Node* temp;
while (current != NULL &&
current->next != NULL)
{
/* If current is smaller than max,
then delete current */
if (current->next->data <
maxnode->data)
{
temp = current->next;
current->next = temp->next;
free(temp);
}
/* If current is greater than max,
then update max and move current */
else
{
current = current->next;
maxnode = current;
}
}
}
/* Utility function to insert a node
at the beginning */
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;
}
/* Utility function to reverse a
linked list */
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;
}
/* Utility function to print a
linked list */
void printList(struct Node* head)
{
while (head != NULL)
{
cout << " " << head->data ;
head = head->next;
}
cout << "" ;
}
// Driver code
int main()
{
struct Node* head = NULL;
/* Create following linked list
12->15->10->11->5->6->2->3 */
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;
}
// This code is contributed by shivanisinghss2110
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!
Similar Reads
C++ Program To Delete N Nodes After M Nodes Of A Linked List Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.Difficulty Level: Rookie Examples: Input: M = 2, N = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked L
3 min read
C++ Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm
3 min read
C++ Program For Writing A Function To Delete A Linked List Algorithm For C++:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C++ // C++ program to delete a linked list #include <bits/stdc++.h> using namespace std
2 min read
Print all paths of the Binary Tree with maximum element in each path greater than or equal to K Given a binary tree and an integer K, the task is to print the paths from root to leaf with the maximum element greater than or equal to K. Print -1 if there exists no such path.Examples: Input: K = 25, 10 / \ 5 8 / \ / \ 29 2 1 98 / \ 20 50 Output: (10, 5, 29, 20), (10, 8, 98, 50) Explanation: The
12 min read
Deletion of a given node K in a Binary Tree using Level Order Traversal Given a binary tree and a node K, the task is to delete the node K from it by making sure that tree shrinks from the bottom (i.e. the deleted node is replaced by bottom-most and rightmost node) using Level Order Traversal. Examples: Input: K = 8, Tree = Output: Explanation: Please Refer below for ex
13 min read