1019. Next Greater Node In Linked List LeetCode Solution
In this guide, you will get 1019. Next Greater Node In Linked List LeetCode Solution with the best time and space complexity. The solution to Next Greater Node In Linked List problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Next Greater Node In Linked List solution in C++
- Next Greater Node In Linked List solution in Java
- Next Greater Node In Linked List solution in Python
- Additional Resources
Problem Statement of Next Greater Node In Linked List
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
Complexity Analysis
- Time Complexity:
- Space Complexity:
1019. Next Greater Node In Linked List LeetCode Solution in C++
class Solution {
public:
vector<int> nextLargerNodes(ListNode* head) {
vector<int> ans;
stack<int> stack;
for (; head; head = head->next) {
while (!stack.empty() && head->val > ans[stack.top()]) {
int index = stack.top();
stack.pop();
ans[index] = head->val;
}
stack.push(ans.size());
ans.push_back(head->val);
}
for (; !stack.empty(); stack.pop())
ans[stack.top()] = 0;
return ans;
}
};
/* code provided by PROGIEZ */
1019. Next Greater Node In Linked List LeetCode Solution in Java
class Solution {
public int[] nextLargerNodes(ListNode head) {
List<Integer> ans = new ArrayList<>();
Deque<Integer> stack = new ArrayDeque<>();
for (; head != null; head = head.next) {
while (!stack.isEmpty() && head.val > ans.get(stack.peek())) {
int index = stack.pop();
ans.set(index, head.val);
}
stack.push(ans.size());
ans.add(head.val);
}
while (!stack.isEmpty())
ans.set(stack.pop(), 0);
return ans.stream().mapToInt(Integer::intValue).toArray();
}
}
// code provided by PROGIEZ
1019. Next Greater Node In Linked List LeetCode Solution in Python
class Solution:
def nextLargerNodes(self, head: ListNode) -> list[int]:
ans = []
stack = []
while head:
while stack and head.val > ans[stack[-1]]:
index = stack.pop()
ans[index] = head.val
stack.append(len(ans))
ans.append(head.val)
head = head.next
for i in stack:
ans[i] = 0
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.