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
- Create new linked list from two given linked list with greater element at each node
- Replace each node with its Surpasser Count in Linked List
- Remove every k-th node of the linked list
- Delete every Kth node from circular linked list
- Rotate the sub-list of a linked list from position M to N to the right by K places
- 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 nodes which have a greater value on right side using recursion
- Delete nodes which have a greater value on right 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 Linked List
- 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
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into 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.