Losing head/tail while rewiring
Wrong move: Pointer updates overwrite references before they are saved.
Usually fails on: List becomes disconnected mid-operation.
Fix: Store next pointers first and use a dummy head for safer joins.
Move from brute-force thinking to an efficient approach using linked list strategy.
Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.
Example 1:
Input: head = [-10,-3,0,5,9] Output: [0,-3,9,-10,null,5] Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
Example 2:
Input: head = [] Output: []
Constraints:
head is in the range [0, 2 * 104].-105 <= Node.val <= 105Problem summary: Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Linked List · Tree
[-10,-3,0,5,9]
[]
convert-sorted-array-to-binary-search-tree)create-binary-tree-from-descriptions)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #109: Convert Sorted List to Binary Search Tree
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<Integer> nums = new ArrayList<>();
public TreeNode sortedListToBST(ListNode head) {
for (; head != null; head = head.next) {
nums.add(head.val);
}
return dfs(0, nums.size() - 1);
}
private TreeNode dfs(int i, int j) {
if (i > j) {
return null;
}
int mid = (i + j) >> 1;
TreeNode left = dfs(i, mid - 1);
TreeNode right = dfs(mid + 1, j);
return new TreeNode(nums.get(mid), left, right);
}
}
// Accepted solution for LeetCode #109: Convert Sorted List to Binary Search Tree
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func sortedListToBST(head *ListNode) *TreeNode {
nums := []int{}
for ; head != nil; head = head.Next {
nums = append(nums, head.Val)
}
var dfs func(i, j int) *TreeNode
dfs = func(i, j int) *TreeNode {
if i > j {
return nil
}
mid := (i + j) >> 1
left := dfs(i, mid-1)
right := dfs(mid+1, j)
return &TreeNode{nums[mid], left, right}
}
return dfs(0, len(nums)-1)
}
# Accepted solution for LeetCode #109: Convert Sorted List to Binary Search Tree
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
def dfs(i: int, j: int) -> Optional[TreeNode]:
if i > j:
return None
mid = (i + j) >> 1
l, r = dfs(i, mid - 1), dfs(mid + 1, j)
return TreeNode(nums[mid], l, r)
nums = []
while head:
nums.append(head.val)
head = head.next
return dfs(0, len(nums) - 1)
// Accepted solution for LeetCode #109: Convert Sorted List to Binary Search Tree
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn sorted_list_to_bst(head: Option<Box<ListNode>>) -> Option<Rc<RefCell<TreeNode>>> {
let mut nums = Vec::new();
let mut current = head;
while let Some(node) = current {
nums.push(node.val);
current = node.next;
}
fn dfs(nums: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {
if nums.is_empty() {
return None;
}
let mid = nums.len() / 2;
Some(Rc::new(RefCell::new(TreeNode {
val: nums[mid],
left: dfs(&nums[..mid]),
right: dfs(&nums[mid + 1..]),
})))
}
dfs(&nums)
}
}
// Accepted solution for LeetCode #109: Convert Sorted List to Binary Search Tree
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function sortedListToBST(head: ListNode | null): TreeNode | null {
const nums: number[] = [];
for (; head; head = head.next) {
nums.push(head.val);
}
const dfs = (i: number, j: number): TreeNode | null => {
if (i > j) {
return null;
}
const mid = (i + j) >> 1;
const left = dfs(i, mid - 1);
const right = dfs(mid + 1, j);
return new TreeNode(nums[mid], left, right);
};
return dfs(0, nums.length - 1);
}
Use this to step through a reusable interview workflow for this problem.
Copy all n nodes into an array (O(n) time and space), then use array indexing for random access. Operations like reversal or middle-finding become trivial with indices, but the O(n) extra space defeats the purpose of using a linked list.
Most linked list operations traverse the list once (O(n)) and re-wire pointers in-place (O(1) extra space). The brute force often copies nodes to an array to enable random access, costing O(n) space. In-place pointer manipulation eliminates that.
Review these before coding to avoid predictable interview regressions.
Wrong move: Pointer updates overwrite references before they are saved.
Usually fails on: List becomes disconnected mid-operation.
Fix: Store next pointers first and use a dummy head for safer joins.
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.