# Replace every node of a Linked list with the greatest element on right side

Given a linked list, the task is to find the Next Greater Element for every node of the linked list.
Note: For nodes with no next greater element, store -1 in the result.

Examples:

Input: linked list = [2, 1, 5]
Output: [5, 5, -1]

Input: linked list = [2, 7, 4, 3, 5]
Output: [7, -1, 5, 5, -1]

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
To solve the problem mentioned above the main idea is to use a Stack Data Structure.

• Iterate through the linked list and insert the value and position of elements of linked list into a stack.
• Initialize result vector with -1 for every node.
• Update previous node’s value while current node’s value is greater than previous nodes and pop the value from stack after updating.

Below is the implementation of the above approach:

## C++

 `// C++ Program to find the ` `// Next Greater Element for ` `// a Linked List ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Linked List Node ` `struct` `Node { ` `    ``int` `val; ` `    ``struct` `Node* next; ` `}; ` ` `  `// Function to print ` `// next greater element ` `vector<``int``> nextLargerNodes( ` `    ``struct` `Node* head) ` `{ ` `    ``int` `cur_pos = 0; ` ` `  `    ``stack > arr; ` ` `  `    ``vector<``int``> res; ` ` `  `    ``// Iterate for all ` `    ``// element in linked list ` `    ``while` `(head) { ` ` `  `        ``// Initialize every ` `        ``// position with 0 ` `        ``res.push_back(-1); ` ` `  `        ``// Check if current value is ` `        ``// greater then update previous ` `        ``while` `( ` `            ``!arr.empty() ` `            ``&& arr.top().second ` `                   ``< head->val) { ` ` `  `            ``res[arr.top().first] ` `                ``= head->val; ` `            ``arr.pop(); ` `        ``} ` ` `  `        ``arr.push(make_pair( ` `            ``cur_pos, ` `            ``head->val)); ` ` `  `        ``cur_pos++; ` ` `  `        ``// Increment the head pointer ` `        ``head = head->next; ` `    ``} ` ` `  `    ``// Return the final result ` `    ``return` `res; ` `} ` ` `  `// Utility function to ` `// create a new node ` `Node* newNode(``int` `val) ` `{ ` `    ``struct` `Node* temp = ``new` `Node; ` `    ``temp->val = val; ` `    ``temp->next = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``struct` `Node* head = newNode(2); ` `    ``head->next = newNode(7); ` `    ``head->next->next = newNode(4); ` `    ``head->next->next->next = newNode(3); ` `    ``head->next->next->next->next = newNode(5); ` ` `  `    ``vector<``int``> ans; ` ` `  `    ``ans = nextLargerNodes(head); ` ` `  `    ``for` `(``int` `i = 0; i < ans.size(); i++) { ` `        ``cout << ans[i] << ``", "``; ` `    ``} ` `} `

Output:

```7, -1, 5, 5, -1,
```

Time Complexity: O(N)
Auxiliary Space Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.