344. Point Arbit Pointer to Greatest Value Right Side Node

EasyLinked ListLinked ListGreedySuffix Maximum

Given a singly linked list where each node has an arbit pointer, set each node's arbit pointer to the node with the maximum value among all nodes to its right.

If no node exists to the right, set arbit to NULL (return -1).

Return an array of arbit pointer values (-1 for NULL).

Input: Head of a singly linked list.

Output: Array of integers: arbit pointer value for each node (-1 if no right node).

Examples

Example 1
Input: [5,10,40,30,28]
Output: [40,40,30,28,-1]
Explanation: Node5->max right=40; Node10->max right=40; Node40->max right=30; Node30->28; Node28->-1.
Example 2
Input: [1,2,3,4,5]
Output: [5,5,5,5,-1]
Explanation: Increasing: each node's max-right is always the last value.
Example 3
Input: [5,4,3,2,1]
Output: [4,3,2,1,-1]
Explanation: Decreasing: each node's max-right is the immediate next.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →