def toTree(head: ListNode, tail: TreeNode): if head == tail: return None slow, fast = head, head while fast != tail and fast.next != tail: fast = fast.next.next slow = slow.next root = TreeNode(slow.val) root.left = toTree(head, slow) root.right = toTree(slow.next, tail) return root