# Delete all the nodes from the list that are greater than x

Given a linked list, the problem is to delete all the nodes from the list that are greater than the specified value **x**.

Examples:

Input : list: 7->3->4->8->5->1 x = 6 Output : 3->4->5->1 Input : list: 1->8->7->3->7->10 x = 7 Output : 1->7->3->7

**Source: **Microsoft Interview Experience | Set 169.

**Approach:** This is mainly a variation of the post which deletes first occurrence of a given key.

We need to first check for all occurrences at head node which are greater than ‘x’, delete them and change the head node appropriately. Then we need to check for all occurrences inside a loop and delete them one by one.

`// C++ implementation to delete all the nodes from the list ` `// that are greater than the specified value x ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// structure of a node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `Node* next; ` `}; ` ` ` `// function to get a new node ` `Node* getNode(` `int` `data) ` `{ ` ` ` `Node* newNode = ` `new` `Node; ` ` ` `newNode->data = data; ` ` ` `newNode->next = NULL; ` ` ` `return` `newNode; ` `} ` ` ` `// function to delete all the nodes from the list ` `// that are greater than the specified value x ` `void` `deleteGreaterNodes(Node** head_ref, ` `int` `x) ` `{ ` ` ` `Node *temp = *head_ref, *prev; ` ` ` ` ` `// If head node itself holds the value greater than 'x' ` ` ` `while` `(temp != NULL && temp->data > x) { ` ` ` `*head_ref = temp->next; ` `// Changed head ` ` ` `free` `(temp); ` `// free old head ` ` ` `temp = *head_ref; ` `// Change temp ` ` ` `} ` ` ` ` ` `// Delete occurrences other than head ` ` ` `while` `(temp != NULL) { ` ` ` ` ` `// Search for the node to be deleted, ` ` ` `// keep track of the previous node as we ` ` ` `// need to change 'prev->next' ` ` ` `while` `(temp != NULL && temp->data <= x) { ` ` ` `prev = temp; ` ` ` `temp = temp->next; ` ` ` `} ` ` ` ` ` `// If required value node was not present ` ` ` `// in linked list ` ` ` `if` `(temp == NULL) ` ` ` `return` `; ` ` ` ` ` `// Unlink the node from linked list ` ` ` `prev->next = temp->next; ` ` ` ` ` `delete` `temp; ` `// Free memory ` ` ` ` ` `// Update Temp for next iteration of ` ` ` `// outer loop ` ` ` `temp = prev->next; ` ` ` `} ` `} ` ` ` `// function to a print a linked list ` `void` `printList(Node* head) ` `{ ` ` ` `while` `(head) { ` ` ` `cout << head->data << ` `" "` `; ` ` ` `head = head->next; ` ` ` `} ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `// Create list: 7->3->4->8->5->1 ` ` ` `Node* head = getNode(7); ` ` ` `head->next = getNode(3); ` ` ` `head->next->next = getNode(4); ` ` ` `head->next->next->next = getNode(8); ` ` ` `head->next->next->next->next = getNode(5); ` ` ` `head->next->next->next->next->next = getNode(1); ` ` ` ` ` `int` `x = 6; ` ` ` ` ` `cout << ` `"Original List: "` `; ` ` ` `printList(head); ` ` ` ` ` `deleteGreaterNodes(&head, x); ` ` ` ` ` `cout << ` `"\nModified List: "` `; ` ` ` `printList(head); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Original List: 7 3 4 8 5 1 Modified List: 3 4 5 1

Time Complexity: O(n).

## Recommended Posts:

- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete linked list nodes which have a greater value on left side
- Delete nodes which have a greater value on right side
- Delete nodes which have a greater value on right side using recursion
- Delete N nodes after M nodes of a linked list
- Delete all the nodes from the list which are less than K
- Delete all the nodes from the list which are divisible by any given number K
- Delete all the even nodes of a Circular Linked List
- Delete all the even nodes from a Doubly Linked List
- Delete alternate nodes of a Linked List
- Delete all Prime Nodes from a Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Delete all odd or even positioned nodes from Circular Linked List
- Delete all Prime Nodes from a Doubly Linked List
- Delete all nodes from the doubly linked list which are divisible by K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.