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:
n.1 <= n <= 1041 <= Node.val <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find, for each item in the list, the first item that comes later in the list and is bigger. The brute force method just checks every possible later item for each item in the list to see if it's bigger.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def next_larger_nodes_brute_force(head):
values = []
current_node = head
while current_node:
values.append(current_node.val)
current_node = current_node.next
result = [0] * len(values)
for i in range(len(values)):
# Need to find the next greater element for each node
for j in range(i + 1, len(values)):
# Check all subsequent nodes
if values[j] > values[i]:
result[i] = values[j]
break
return resultTo find the next greater node, we'll use a special tool to keep track of promising candidates for the next biggest value. This tool helps us efficiently skip over smaller values and find the answer quickly as we go through the list.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, value=0, next_node=None):
self.value = value
self.next_node = next_node
def next_larger_nodes(head):
result_list = []
current_node = head
while current_node:
result_list.append(current_node.value)
current_node = current_node.next_node
answer = [0] * len(result_list)
stack = []
for i in range(len(result_list)):
#Process stack for values less than current
while stack and result_list[stack[-1]] < result_list[i]:
index = stack.pop()
answer[index] = result_list[i]
# Store current index in stack
stack.append(i)
return answer| Case | How to Handle |
|---|---|
| Empty linked list | Return an empty array immediately since there are no nodes to process. |
| Linked list with only one node | Return an array containing only 0, as a single node cannot have a next greater node. |
| Linked list where all nodes have the same value | Return an array of the same size as the linked list, filled with zeros because no node will have a strictly greater value than any subsequent node. |
| Linked list with values in strictly decreasing order | Return an array of the same size as the linked list, filled with zeros, because no node will have a greater value than any subsequent node. |
| Large linked list (scalability) | The stack-based approach should handle large lists efficiently due to its linear time complexity, O(n). |
| Linked list containing duplicate values | The algorithm correctly identifies the *next* greater element regardless of duplicate values, using the stack to track potential next greater elements in order. |
| Linked list with negative values | The comparison operation works correctly with negative numbers, finding the next greater value regardless of sign. |
| Linked list with very large integer values that may cause integer overflow in some languages | Ensure the language used can handle the range of possible node values to prevent integer overflow during comparison or when creating the result array. |