* Definition for singly-linked list.
* public class ListNode {
* ListNode(int x) { val = x; }
* Definition for a binary tree node.
* public class TreeNode {
* TreeNode(int x) { val = x; }
public TreeNode sortedListToBST(ListNode head) {
return convertToBST(head, null);
public TreeNode convertToBST(ListNode start, ListNode end) {
if (start == null || start == end) {
while (fast.next != end && fast.next.next != end) {
TreeNode root = new TreeNode(slow.val);
root.left = convertToBST(start, slow);
root.right = convertToBST(slow.next, end);