# Delete linked list nodes which have a greater value on left side

Given a singly linked list, the task is to remove all the nodes which have a greater value on left side.

**Examples:**

Input:12->15->10->11->5->6->2->3Output:Modified Linked List = 12 15Input:25->15->6->48->12->5->16->14Output:Modified Linked List = 14 16 48

**Approach:**

- Initialize the maximum with head node.
- Traverse the list.
- Check if the next node is greater than max_node then update the value of max_node and move to the next node.
- Else delete the next node.

Below is the implementation of above approach:

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Structure of a linked list node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// Function to Delete nodes which have ` `// greater value node(s) on right side ` `void` `delNodes(` `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 greater than max, ` ` ` `// then update max and move current ` ` ` `if` `(current->next->data >= maxnode->data) { ` ` ` `current = current->next; ` ` ` `maxnode = current; ` ` ` `} ` ` ` ` ` `// If current is smaller than max, then delete current ` ` ` `else` `{ ` ` ` `temp = current->next; ` ` ` `current->next = temp->next; ` ` ` `free` `(temp); ` ` ` `} ` ` ` `} ` `} ` ` ` `/* Utility function to insert a node at the begining */` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` `new` `Node; ` ` ` `new_node->data = new_data; ` ` ` `new_node->next = *head_ref; ` ` ` `*head_ref = new_node; ` `} ` ` ` `/* Utility function to print a linked list */` `void` `printList(` `struct` `Node* head) ` `{ ` ` ` `while` `(head != NULL) { ` ` ` `cout << head->data << ` `" "` `; ` ` ` `head = head->next; ` ` ` `} ` ` ` `cout << endl; ` `} ` ` ` `/* Driver program to test above functions */` `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); ` ` ` ` ` `printf` `(` `"Given Linked List \n"` `); ` ` ` `printList(head); ` ` ` ` ` `delNodes(head); ` ` ` ` ` `printf` `(` `"Modified Linked List \n"` `); ` ` ` `printList(head); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Given Linked List 12 15 10 11 5 6 2 3 Modified Linked List 12 15

## Recommended Posts:

- Delete linked list nodes which have a Lesser Value on Left Side
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete nodes which have a greater value on right side
- Delete nodes which have a greater value on right side using recursion
- Delete all the nodes from the list that are greater than x
- Delete N nodes after M nodes of a linked list
- Delete alternate nodes of a Linked List
- Delete all the even nodes of a Circular Linked List
- Delete all the even nodes from a Doubly Linked List
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete all 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
- Delete all Non-Prime Nodes from a Singly Linked List

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.