public static TreeNode sortedListToBST(ListNode head) {//AC
        if (head == null) {
            return null;
        }
        ListNode p = head;
        int len = 0;
        while (p != null) {
            len++;
            p = p.next;
        }
        int num[] = new int[len];
        p = head;
        len = 0;
        while (p != null) {
            num[len] = p.val;
            len++;
            p = p.next;
        }
        return buildTree(num, 0, len-1);
    }