Distance Between Critical Points
A node is critical if it is not the first or last node and its value is strictly greater than both neighbors or strictly smaller than both neighbors. Return [closest, farthest]: the smallest and the largest distance (difference in indexes) between any two critical nodes. If there are fewer than two critical nodes, return [-1, -1]. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [5,3,1,2,5,1,2]
returns [1,3]
Critical at indexes 2, 4, 5. Closest 1, farthest 3.
Example 2
head [1,3,2]
returns [-1,-1]
Only one critical node.
Constraints
0 <= number of nodes <= 1001 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.