public TreeNode sortedListToBST(ListNode head) {
        int count = 0;
        ListNode p = head;
        while (p != null) {
            count++;
            p = p.next;
        }

        int[] num = new int[count];
        p = head;
        count = 0;
        while (p != null) {
            num[count] = p.val;
            count++;
            p = p.next;
        }

        if (num.length == 0)
            return null;
        return BST(num, 0, num.length - 1);

    }