Delete all the nodes from the list which are less than K
Given a Linked List and a key K. The task is to write a program to delete all the nodes from the list that are lesser than the key K.
Examples:
Input :12->15->9->11->5->6
K = 9
Output : 12 -> 15 -> 9 -> 11
Input : 13->4->16->9->22->45->5->16->6
K = 10
Output : 13 -> 16 -> 22 -> 45 -> 16
Approach: The approach is similar to deleting all nodes from the list which are greater than the given key.
There are two possible cases:
- Head node holds a value less than K: First check for all occurrences at head node which are lesser than ‘K’, delete them and change the head node appropriately.
- Some intermediate node holds value less than k: Start traversing from head and check if current node’s value is less than K. If yes then delete that node and move forward in the list.
Below is the implementation of the above approach:
// C++ program to delete all the nodes from the list // that are lesser than the specified value K #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 lesser than the specified value K void deleteLesserNodes(Node** head_ref, int K) { Node *temp = *head_ref, *prev; // If head node itself holds the value lessser than 'K' while (temp != NULL && temp->data < K) { *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 >= K) { 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 code int main() { // Create list: 12->15->9->11->5->6 Node* head = getNode(12); head->next = getNode(15); head->next->next = getNode(9); head->next->next->next = getNode(11); head->next->next->next->next = getNode(5); head->next->next->next->next->next = getNode(6); int K = 9; cout << "Initial List: "; printList(head); deleteLesserNodes(&head;, K); cout << "\nFinal List: "; printList(head); return 0; } |
chevron_right
filter_none
Output:
Initial List: 12 15 9 11 5 6 Final List: 12 15 9 11
Time Complexity: O(N)
Recommended Posts:
- Delete N nodes after M nodes of a linked list
- Delete all the nodes from the list that are greater than x
- Delete all the nodes from the list which are divisible by any given number K
- 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 nodes from the doubly linked list which are divisible by K
- Delete all Prime Nodes from a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- 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 Circular Singly Linked List
- Delete linked list nodes which have a Lesser Value on Left Side
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.



