Delete all the nodes from the list that are greater than x
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
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.
Output:
Original List: 7 3 4 8 5 1 Modified List: 3 4 5 1
Time Complexity: O(n).
Comments
Post a Comment