Convert Sorted List to Binary Search Tree

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:

One possible balanced BST.

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

Edge case with a single-element array.

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

Edge case with an empty linked list. When there are no nodes to convert, the resulting BST is also empty (null root).

Constraints

  • 0 ≤ number of nodes ≤ 2 * 10⁴
  • -10⁵ ≤ Node.val ≤ 10⁵

Ready to solve this problem?

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