Convert Sorted List to BST

Medium

Description

Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced BST.

Examples

Input:head = [-10,-3,0,5,9]
Output:[0,-3,9,-10,null,5]
Explanation:

Balanced BST.

Input:head = []
Output:[]
Explanation:

Edge case with empty result.

Input:head = [1,3,5,7,9,11]
Output:[5,3,9,1,null,7,11]
Explanation:

For an even number of nodes, choosing the second middle element (5) as root. The left subtree contains [1,3] with 3 as root, and right subtree contains [7,9,11] with 9 as root, creating a height-balanced BST.

Constraints

  • 0 ≤ nodes ≤ 2 × 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!