class Solution:
def sortedListToBST(self, head):
"""
:type head: ListNode
:rtype: TreeNode
"""
def getMid(head):
if not head or not head.next: return None
fast, slow, prev = head, head, None
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
prev.next = None
return slow
if not head: return None
if not head.next: return TreeNode(head.val)
mid = getMid(head)
rightHead = mid.next
mid.next = None
root = TreeNode(mid.val)
root.left = self.sortedListToBST(head)
root.right = self.sortedListToBST(rightHead)
return root