Given a singly linked list, remove all the nodes which have a greater value on the right side.
a) The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side.
When we examine 12, we see that after 12 there is one node with a value greater than 12 (i.e. 15), so we delete 12.
When we examine 15, we find no node after 15 that has a value greater than 15 so we keep this node.
When we go like this, we get 15->6->3
b) The list 10->20->30->40->50->60->NULL should be changed to 60->NULL. Note that 10, 20, 30, 40 and 50 have been deleted because they all have a greater value on the right side.
c) The list 60->50->40->30->20->10->NULL should not be changed.
Approach: We have already solved this problem by using 2 loops and reversing linked list in the post Delete nodes which have a greater value on right side
Here we will discuss the solution without reversing the list. We will use recursion to solve this problem in which the base case would be when the head is pointing to NULL. Else we would be recursively calling function for the next node and updating max value if currentNode->data > currentMax. In this way, the whole list would be updated.
Below is the implementation of the above approach:
# Python3 program to reverse a linked
# list using a stack
# Link list node
def __init__(self, data, next):
self.data = data
self.next = next
self.head = None
# Function to push a new Node in
# the linked list
def push(self, new_data):
new_node = Node(new_data, self.head)
self.head = new_node
# Function to delete nodes which have a node
# with greater value node on left side
def delNodes(self, head):
# Base case
if head == None:
head.next = self.delNodes(head.next)
if head.data < Max: return head.next Max = max(head.data, Max) return head # Function to print the Linked list def printList(self): curr = self.head while curr: print(curr.data, end = " ") curr = curr.next print() # Driver Code if __name__ == "__main__": # Start with the empty list linkedList = LinkedList() # Create following linked list # 12->15->10->11->5->6->2->3
print(“Given Linked List”)
Max = float(‘-inf’)
linkedList.head = linkedList.delNodes(linkedList.head)
print(“Modified Linked List”)
# This code is contributed by Rituraj Jain
Given Linked List 12 15 10 11 5 6 2 3 Modified Linked List 15 11 6 3
- Delete nodes which have a greater value on right side
- Delete linked list nodes which have a greater value on left side
- Delete linked list nodes which have a Lesser Value on Left Side
- Delete all the nodes from the list that are greater than x
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete N nodes after M nodes of a linked list
- Delete a linked list using recursion
- 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 alternate nodes of a Linked List
- Delete all the even nodes from a Doubly Linked List
- Delete all the even nodes of a Circular Linked List
- Print alternate nodes of a linked list using recursion
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete all Prime Nodes from a Doubly 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 email@example.com. 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.
Improved By : rituraj_jain