Let slow.next to be the pivot. In this way, it is easy to set slow.next = null to cut the list.

```
public static TreeNode sortedListToBST(ListNode head) {
if (head == null) {
return null;
}
if (head.next == null) {
return new TreeNode(head.val);
}
ListNode slow = head, fast = head.next; // pivot should be slow.next. In this way, it is easy to set slow.next = null
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
TreeNode node = new TreeNode(slow.next.val); // pivot is slow.next
ListNode next = slow.next.next;
slow.next = null; // cut the list.
node.left = sortedListToBST(head);
node.right = sortedListToBST(next);
return node;
}
```