Given a linked list, the problem is to delete all the nodes from the list that are greater than the specified value x.
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
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.
Original List: 7 3 4 8 5 1 Modified List: 3 4 5 1
Time Complexity: O(n).
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Mu Sigma Interview On Campus
- Bank Of America (BA Continuum India Pvt. Ltd.) Campus Recruitment
- Bubble Sort On Doubly Linked List
- Reverse nodes of a linked list without affecting the special characters
- Applications of linked list data structure
- Amazon SDE-2 Interview Experience
- Flatten a binary tree into linked list | Set-2
- Merge two sorted linked list without duplicates
- KLA Tencor Interview Experience | Set 3
- Find first node of loop in a linked list