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]

**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 <bits/stdc++.h> ` `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<pair<` `int` `, ` `int` `> > 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] << ` `", "` `; ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Point arbit pointer to greatest value right side node in a linked list
- Replace each node with its Surpasser Count in Linked List
- Create new linked list from two given linked list with greater element at each node
- Delete linked list nodes which have a Lesser Value on Left Side
- Delete linked list nodes which have a greater value on left side
- Replace nodes with duplicates in linked list
- Replace even nodes of a doubly linked list with the elements of array
- Swap Kth node from beginning with Kth node from end in a Doubly Linked List
- Create a linked list from two linked lists by choosing max element at each position
- Swap Kth node from beginning with Kth node from end in a Linked List
- Delete Nth node from the end of the given linked list
- Remove every k-th node of the linked list
- Squareroot(n)-th node in a Linked List
- Linked List | Set 3 (Deleting a node)
- Remove last node of the linked list
- Remove first node of the linked list
- Linked List | Set 2 (Inserting a node)
- Program for n'th node from the end of a Linked List
- Remove Nth node from end of the Linked List
- Delete every Kth node from circular 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 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.