# Share My Easy Understatnd Java Solution.

• ``````public class Solution {
return null;
ListNode temp=null;

//find the mid node
while(fast.next!=null && fast.next.next!=null){
fast = fast.next.next;
temp = slow;
slow = slow.next;
}

if(temp!=null)
temp.next = null; //break the link
else

TreeNode root = new TreeNode(slow.val);
root.right = sortedListToBST(slow.next);
return root;
}
``````

}

• IMHO, you can't modify the original list unless you are allowed to do so

• I think your temp will never equal to null.
Sorry, I got it.

• when the list's size is 1

• Is it O(N^2) time complexity?

• It's possible to accomplish without modifying the original list.

We are already finding the middle node which is the end for the left subtree. So pass on this node recursively instead of breaking the list.

``````public TreeNode sortedListToBST(ListNode head) {
}

private TreeNode sortedListToBST(ListNode start, ListNode end) {

if (start == null || start == end)
return null;

ListNode fast = start;
ListNode slow = start;

while (fast.next != end && fast.next.next != end) {
fast = fast.next.next;
slow = slow.next;
}

TreeNode root = new TreeNode(slow.val);
root.left = sortedListToBST(start, slow);
root.right = sortedListToBST(slow.next, end);

return root;
}``````

• This is the best solution so far. Thanks

• Best solution ever!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.