Next Greater Node In Linked List - Problem

You're given the head of a singly linked list with n nodes. Your mission is to find the next greater element for each node in the list.

For each node, you need to find the first node that comes after it and has a strictly larger value. If no such node exists, the answer for that position should be 0.

Return an integer array answer where answer[i] represents the value of the next greater node for the i-th node (1-indexed). This is a classic example of the "Next Greater Element" pattern, but with the added challenge of working with a linked list structure.

Example: For linked list 2 โ†’ 1 โ†’ 5, the answer would be [5, 5, 0] because:

  • Node 2: Next greater is 5
  • Node 1: Next greater is 5
  • Node 5: No next greater element exists

Input & Output

example_1.py โ€” Basic Case
$ Input: head = [2,1,5]
โ€บ Output: [5,5,0]
๐Ÿ’ก Note: For node with value 2: next greater is 5. For node with value 1: next greater is 5. For node with value 5: no next greater element exists.
example_2.py โ€” Increasing Sequence
$ Input: head = [1,7,5,1,9,2,5,1]
โ€บ Output: [7,9,9,9,0,5,0,0]
๐Ÿ’ก Note: Each element finds its next greater element or gets 0 if none exists. For example, 7's next greater is 9, 5's next greater is 9, etc.
example_3.py โ€” Single Element
$ Input: head = [5]
โ€บ Output: [0]
๐Ÿ’ก Note: Single element has no next element, so result is [0].

Constraints

  • The number of nodes in the linked list is in the range [0, 104]
  • 1 โ‰ค Node.val โ‰ค 109
  • Note: The result should be 1-indexed as per the problem description

Visualization

Tap to expand
Next Greater Element: Monotonic Stack SolutionInput: [2,1,5]215Stack Evolution:i=0: Stack=[0], Process 2i=1: Stack=[0,1], Process 1i=2: Process 5โ†’ 5 > values[1]=1, pop 1, result[1]=5โ†’ 5 > values[0]=2, pop 0, result[0]=5โ†’ Push 2, Stack=[2]Step-by-Step VisualizationStep 1: Process element 2 (index 0)Stack is empty, push index 00โ† Stack: [0]Step 2: Process element 1 (index 1)1 < 2, so push index 101โ† Stack: [0,1]Step 3: Process element 5 (index 2)5 > 1 and 5 > 2, pop both!result[1] = 5, result[0] = 52โ† Stack: [2]Final Result: [5, 5, 0]โ€ข Index 0 (value 2): Next greater is 5โ€ข Index 1 (value 1): Next greater is 5โ€ข Index 2 (value 5): No next greater element (remains in stack)๐Ÿ’ก Key Insight: Each element is pushed and popped at most once โ†’ O(n) time!
Understanding the Visualization
1
Convert to Array
Transform linked list [2โ†’1โ†’5] into array [2,1,5] for easier processing
2
Initialize Stack
Create empty stack to store indices of elements waiting for next greater element
3
Process Elements
For each element, pop smaller elements from stack (they found their answer) and push current index
4
Handle Remaining
Elements still in stack have no next greater element, so their result stays 0
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic stack pattern allows us to solve "next greater element" problems in O(n) time by maintaining pending queries efficiently and resolving multiple queries when we find a larger element.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
63.4K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen