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] +
", "
);
}
}
}