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 the linked list into a stack.
- Initialize the result vector with -1 for every node.
- Update the previous node’s value while the current node’s value is greater than the previous nodes and pop the value from the stack after updating.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
struct Node* next;
};
vector< int > nextLargerNodes(
struct Node* head)
{
int cur_pos = 0;
stack<pair< int , int > > arr;
vector< int > res;
while (head) {
res.push_back(-1);
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++;
head = head->next;
}
return res;
}
Node* newNode( int val)
{
struct Node* temp = new Node;
temp->val = val;
temp->next = NULL;
return temp;
}
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] << ", " ;
}
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int val;
Node next;
};
static class pair
{
int first, second;
public pair( int first,
int second)
{
this .first = first;
this .second = second;
}
} ;
static Vector<Integer>
nextLargerNodes(Node head)
{
int cur_pos = 0 ;
Stack<pair > arr = new Stack<>();
Vector<Integer> res = new Vector<>();
while (head != null )
{
res.add(- 1 );
while (!arr.isEmpty() &&
arr.peek().second <
head.val)
{
res.set(arr.peek().first,
head.val);
arr.pop();
}
arr.add( new pair(cur_pos,
head.val));
cur_pos++;
head = head.next;
}
return res;
}
static Node newNode( int val)
{
Node temp = new Node();
temp.val = val;
temp.next = null ;
return temp;
}
public static void main(String[] args)
{
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<Integer> ans = new Vector<>();
ans = nextLargerNodes(head);
for ( int i = 0 ; i < ans.size(); i++)
{
System.out.print(ans.elementAt(i) + ", " );
}
}
}
|
Python3
class newNode:
def __init__( self , val):
self .val = val
self . next = None
def nextLargerNodes(head):
cur_pos = 0
arr = []
res = []
while (head):
res.append( - 1 )
while ( len (arr) > 0 and
arr[ len (arr) - 1 ][ 1 ] < head.val):
res[arr[ len (arr) - 1 ][ 0 ]] = head.val
arr.remove(arr[ len (arr) - 1 ])
arr.append([cur_pos, head.val])
cur_pos + = 1
head = head. next
return res
if __name__ = = '__main__' :
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 )
ans = nextLargerNodes(head)
for i in range ( len (ans)):
print (ans[i], end = ", " )
|
C#
using System;
using System.Collections.Generic;
class GFG{
class Node
{
public int val;
public Node next;
};
class pair
{
public int first, second;
public pair( int first,
int second)
{
this .first = first;
this .second = second;
}
} ;
static List< int >
nextLargerNodes(Node head)
{
int cur_pos = 0;
Stack<pair > arr = new Stack<pair>();
List< int > res = new List< int >();
while (head != null )
{
res.Add(-1);
while (arr.Count !=0 &&
arr.Peek().second <
head.val)
{
res[arr.Peek().first] =
head.val;
arr.Pop();
}
arr.Push( new pair(cur_pos,
head.val));
cur_pos++;
head = head.next;
}
return res;
}
static Node newNode( int val)
{
Node temp = new Node();
temp.val = val;
temp.next = null ;
return temp;
}
public static void Main(String[] args)
{
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);
List< int > ans = new List< int >();
ans = nextLargerNodes(head);
for ( int i = 0; i < ans.Count; i++)
{
Console.Write(ans[i] + ", " );
}
}
}
|
Javascript
<script>
class newNode{
constructor(val){
this .val = val
this .next = null
}
}
function nextLargerNodes(head){
let cur_pos = 0
let arr = []
let res = []
while (head){
res.push(-1)
while (arr.length> 0 && arr[arr.length- 1][1] < head.val){
res[arr[arr.length- 1][0]] = head.val
arr.pop()
}
arr.push([cur_pos, head.val])
cur_pos += 1
head = head.next
}
return res
}
let head = new newNode(2)
head.next = new newNode(7)
head.next.next = new newNode(4)
head.next.next.next = new newNode(3)
head.next.next.next.next = new newNode(5)
ans = nextLargerNodes(head)
for (let i=0;i<ans.length;i++)
document.write(ans[i], ", " )
</script>
|
Time Complexity: O(N)
Auxiliary Space Complexity: O(N)