A critical point in a linked list is defined as either a local maxima or a local minima. A node is a local maxima if the current node has a value strictly greater than the previous node and the next node. A node is a local minima if the current node has a value strictly smaller than the previous node and the next node. Note that a node can only be a local maxima/minima if there exists both a previous node and a next node. Given a linked list head, return an array of length 2 containing [minDistance, maxDistance] where minDistance is the minimum distance between any two distinct critical points and maxDistance is the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1].
Example 1
Input: head = [5,3,1,2,5,1,2]
Output: [1,3]
Explanation: Critical points at positions 3 (minima), 5 (maxima) and 6 (minima). Closest pair is 5 and 6 (distance 1); farthest is 3 and 6 (distance 3).
Example 2
Input: head = [3,1]
Output: [-1,-1]
Explanation: No node has both a previous and next neighbour, so there are no critical points.
Example 3
Input: head = [1,3,2,2,3,2,2,2,7]
Output: [3,3]
Explanation: Node 2 (value 3, between 1 and 2) and node 5 (value 3, between 2 and 2) are local maxima. Every other node has an equal neighbour or no neighbour on one side. With only these two critical points, both the minimum and maximum distance are 5 - 2 = 3.
Scan the list once, comparing each node with its neighbours. The maximum distance is always between the first and last critical point; the minimum is the smallest gap between consecutive ones.
Run checks all cases above. Submit evaluates all test cases.