public static boolean isMonotonous(List<Integer> list){
        if (list.isEmpty() || list.size() <= 2) return true;
        for (int i=0;i<list.size()-2;i++) {
            if ((list.get(i) <= list.get(i+1) && list.get(i+1) <= list.get(i+2)) || 
                (list.get(i) >= list.get(i+1) && list.get(i+1) >= list.get(i+2))) {
                continue;
            }
            else
                return false;
        }
        return true;
        /* Here's a one line solution but the above is much faster:
         * return (sort(list).equals(list) || sort(list).equals(reverse(list)));
         */
    }