```
void insert(TreeNode* root, int value) {
TreeNode** pos = &root;
while (*pos) {
if (value < (*pos)->val) {
pos = &(*pos)->left;
} else {
pos = &(*pos)->right;
}
}
*pos = new TreeNode(value);
}
TreeNode* sortedListToBST(ListNode *head) {
if (!head) return NULL;
ListNode* current = head;
TreeNode* root = new TreeNode(current->val);
current = current->next;
while(current) {
insert(root, current->val);
current = current->next;
}
return root;
}
```