classSolution {publicTreeNodesortedListToBST(ListNode head) {returnrecursion(head,null); }// tail to mark the position that should not be reached// effective list: from head (include) to tail (not include)privateTreeNoderecursion(ListNode head,ListNode tail){if (head == tail) returnnull;ListNode slow = head;ListNode fast = head;while(fast!=tail &&fast.next!=tail){ slow =slow.next; fast =fast.next.next; }TreeNode node =newTreeNode(slow.val);node.left=recursion(head, slow);node.right=recursion(slow.next, tail);return node; }}