public int[] nodesBetweenCriticalPoints(ListNode head) {
    ListNode pre = head;
    head = head.next;
    Deque<Integer> q = new LinkedList<>();
    int maxDistance = Integer.MIN_VALUE;
    int minDistance = Integer.MAX_VALUE;
    int pos = 0;
    while (head != null && head.next != null) {
        int curVal = head.val;
        int preVal = pre.val;
        int nextVal = head.next.val;
        if ((preVal < curVal && nextVal < curVal) || (preVal > curVal && nextVal > curVal)) {
            if (!q.isEmpty()) {
                maxDistance = Math.max(pos - q.peekFirst(), maxDistance);
                minDistance = Math.min(pos - q.peekLast(), minDistance);
            }
            q.add(pos);
        }
        pre = head;
        head = head.next;
        pos++;
    }
    return new int[]{minDistance == Integer.MAX_VALUE ? -1 : minDistance,maxDistance == Integer.MIN_VALUE ? -1 : maxDistance }
}