Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

1019. Next Greater Node In Linked List

Medium61.5% Acceptance
ArrayLinked ListStack
Asked by:
A
Amazon
ProblemHints (1)Solutions (12)VideosCompanies (2)Notes

Problem Statement

You are given the head of a linked list with n nodes.

For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it.

Return an integer array answer where answer[i] is the value of the next greater node of the ith node (1-indexed). If the ith node does not have a next greater node, set answer[i] = 0.

Example 1:

Input: head = [2,1,5]
Output: [5,5,0]

Example 2:

Input: head = [2,7,4,3,5]
Output: [7,0,5,5,0]

Constraints:

  • The number of nodes in the list is n.
  • 1 <= n <= 104
  • 1 <= Node.val <= 109
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
B
Bloomberg

Approach

In #1019 Next Greater Node In Linked List, the goal is to find the next node with a greater value for each node in a singly linked list. Since linked lists do not support random access, a helpful strategy is to first traverse the list and store node values in an array. This makes it easier to process elements by index.

The optimal technique uses a monotonic decreasing stack. As you iterate through the array, maintain a stack that stores indices of elements whose next greater value has not yet been found. When the current value is greater than the value at the index on top of the stack, you can resolve that index by setting its next greater value. This process continues until the stack condition is restored.

This approach ensures each element is pushed and popped at most once, giving an efficient O(n) time complexity with O(n) extra space for the stack and result array.

Complexity

ApproachTime ComplexitySpace Complexity
Array Conversion + Monotonic StackO(n)O(n)

Video Solution Available

take U forward

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

We can use a stack that stores nodes in monotone decreasing order of value. When we see a node_j with a larger value, every node_i in the stack has next_larger(node_i) = node_j .

Ready to see the solutions?View Solutions

Solutions (12)

Monotonic Stack Approach

This approach leverages a monotonic stack to efficiently find the next greater element for each node in the linked list. By traversing the linked list once, we can maintain a stack to track potentially greater nodes and update results as we traverse.

Time Complexity: O(n), where n is the length of the linked list, because each node is pushed and popped from the stack at most once.
Space Complexity: O(n), needed for storing the result and stack.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4// Definition for singly-linked list.
5struct ListNode {
6    int val;
7

    

Explanation

This C solution uses an array-based stack to capture and process nodes from the linked list. As each node is encountered, it checks the stack for any nodes smaller than the current node. If found, it updates the result set. The code is efficient and operates in O(n) time complexity.

Reverse Traversal with Stack Approach

In this approach, reverse the linked list initially and then traverse, keeping track of the next greatest node using a stack. As this traversal is reversed, use a stack to efficiently find and keep the nearest greatest value.

Time Complexity: O(n)
Space Complexity: O(n)

CC++JavaPythonC#JavaScript
1#


    


Video Solutions

Watch expert explanations and walkthroughs

Next Greater Element | Two Variants | Leetcode

take U forward
22:00265,567 views

Asked By Companies

2 companies
A
Amazon
B
Bloomberg

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Design Phone DirectoryMedium
Design Circular QueueMedium
Design Circular DequeMedium
Design HashSetEasy
More similar problems

Related Topics

ArrayLinked ListStackMonotonic Stack

Problem Stats

Acceptance Rate61.5%
DifficultyMedium
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why convert the linked list to an array in this problem?

Converting the linked list to an array simplifies indexing and traversal. Since stacks typically work with indices for efficient updates, using an array allows you to track positions and update the result array easily while applying the monotonic stack technique.

Is Next Greater Node In Linked List asked in FAANG interviews?

Yes, variations of next greater element problems frequently appear in FAANG and other top tech interviews. This question tests understanding of stacks, monotonic data structures, and efficient linear-time processing of sequences.

What data structure is best for Next Greater Node In Linked List?

A monotonic stack is the most effective data structure for this problem. It helps maintain elements in decreasing order and quickly identifies when a greater value appears. This pattern is commonly used in 'next greater element' problems.

What is the optimal approach for Next Greater Node In Linked List?

The optimal approach converts the linked list into an array and then applies a monotonic decreasing stack to track indices whose next greater value has not yet been found. As you iterate through the array, you resolve elements when a larger value appears. This ensures each element is processed at most twice, making the solution efficient.

struct
ListNode
*
next
;
8
}
;
9
10
struct
ListNode
*
*
createStack
(
int
n
)
{
11
return
(
struct
ListNode
*
*
)
malloc
(
n
*
sizeof
(
struct
ListNode
*
)
)
;
12
}
13
14
int
*
nextLargerNodes
(
struct
ListNode
*
head
,
int
*
returnSize
)
{
15
int
*
results
=
(
int
*
)
malloc
(
10000
*
sizeof
(
int
)
)
;
16
struct
ListNode
*
*
stack
=
createStack
(
10000
)
;
17
int
*
stackIndex
=
(
int
*
)
malloc
(
10000
*
sizeof
(
int
)
)
;
18
int
top
=
-
1
,
index
=
0
;
19
struct
ListNode
*
current
=
head
;
20
21
while
(
current
)
{
22
while
(
top
>=
0
&&
stack
[
top
]
->
val
<
current
->
val
)
{
23
results
[
stackIndex
[
top
--
]
]
=
current
->
val
;
24
}
25
stack
[
++
top
]
=
current
;
26
stackIndex
[
top
]
=
index
;
27
results
[
index
++
]
=
0
;
28
current
=
current
->
next
;
29
}
30
*
returnSize
=
index
;
31
return
results
;
32
}
33
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
struct
ListNode
{
5
int
val
;
6
struct
ListNode
*
next
;
7
}
;
8
9
struct
ListNode
*
reverseList
(
struct
ListNode
*
head
)
{
10
struct
ListNode
*
prev
=
NULL
;
11
struct
ListNode
*
current
=
head
;
12
while
(
current
)
{
13
struct
ListNode
*
next_node
=
current
->
next
;
14
current
->
next
=
prev
;
15
prev
=
current
;
16
current
=
next_node
;
17
}
18
return
prev
;
19
}
20
21
int
*
nextLargerNodes
(
struct
ListNode
*
head
,
int
*
returnSize
)
{
22
head
=
reverseList
(
head
)
;
23
int
*
result
=
(
int
*
)
malloc
(
10000
*
sizeof
(
int
)
)
;
24
int
*
stack
=
(
int
*
)
malloc
(
10000
*
sizeof
(
int
)
)
;
25
int
top
=
-
1
,
idx
=
0
;
26
27
struct
ListNode
*
current
=
head
;
28
while
(
current
)
{
29
while
(
top
>=
0
&&
stack
[
top
]
<=
current
->
val
)
{
30
top
--
;
31
}
32
result
[
idx
++
]
=
(
top
==
-
1
)
?
0
:
stack
[
top
]
;
33
stack
[
++
top
]
=
current
->
val
;
34
current
=
current
->
next
;
35
}
36
37
for
(
int
i
=
0
,
j
=
idx
-
1
;
i
<
j
;
i
++
,
j
--
)
{
// Reverse the result back
38
int
temp
=
result
[
i
]
;
39
result
[
i
]
=
result
[
j
]
;
40
result
[
j
]
=
temp
;
41
}
42
43
*
returnSize
=
idx
;
44
return
result
;
45
}
46

Explanation

In this approach, the linked list is reversed first. As we traverse from the new start, greater elements are stacked and compared for finding the next greater node. At the end, a final reversal of results provides the answer.