You are given the head of a linked list. Remove every node that has a node with a strictly greater value anywhere to its right, and return the head of the modified list.
You are given the head of a linked list. Remove every node that has a node with a strictly greater value anywhere to its right, and return the head of the modified list.
Example 1:
Input: {"head":[5,2,13,3,8]}
Output: [13,8]
Constraints:
The number of nodes in the list is in the range [1,105].
1≤Node.val≤105
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"head":[5,2,13,3,8]}
Walk the list once, keeping a stack of survivors whose values never increase from bottom to top. A node survives only if nothing bigger comes after it.
▼ node: being processed■ on the stack (survivors so far)✕ popped: a bigger node came later