public TreeNode sortedListToBST(ListNode head) {
        // Start typing your Java solution below
        // DO NOT write main() function

        if (head == null) {
            return null;
        }

        int size = 1;
        ListNode n = head;
        while (n.next != null) {
            size++;
            n = n.next;
        }

        TreeNode result = buildTree(0, size - 1);

        inOrder(result, head);

        return result;
    }