My Python Solution


  • 0
    W
    class Solution(object):
        
        def sortedListToBST(self, head):
            if not head:
                return
            
            if not head.next:
                return TreeNode(head.val)
    
            fast = head
            slow = None
            while fast and fast.next:
                fast = fast.next.next
                slow = head if slow is None else slow.next
    
            node = TreeNode(slow.next.val)
            tmp = slow.next.next
            slow.next = None
            node.left = self.sortedListToBST(head)
            node.right = self.sortedListToBST(tmp)
            return node
    

Log in to reply
 

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