```
public TreeNode sortedListToBST(ListNode head) {
HashMap<Integer,TreeNode> map = new HashMap<Integer,TreeNode>();
ListNode p ;
int i = 0;
for(p=head ; p!=null ; p = p.next){
map.put(i, new TreeNode(p.val));
i++;
}
return buildTree(map,0,i-1);
}
public static TreeNode buildTree(HashMap<Integer,TreeNode> map, int start , int end){
if(start<=end){
int mid = (start+end)/2;
TreeNode t = map.get(mid);
t.left = buildTree(map,start,mid-1);
t.right = buildTree(map,mid+1,end);
return t;
}
else return null;
}
```