# Description

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

The original problem is here.

The original code is here.

# My Solution

I solve this problem in C++, as below:

``````/*
*Convert Sorted List to Binary Search Tree
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
*/
#include<iostream>
#include<stack>
#include<stdlib.h>
using namespace std;

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
return NULL;
TreeNode *root = new TreeNode(-1);
return root;
}

ListNode *before, *slow, *fast;
while(fast->next!=NULL && fast->next->next!=NULL){
before = before->next;
slow = slow->next;
fast = fast->next->next;
}
if(fast->next == NULL){
fast = slow;
before->next = NULL;
}
else{
fast = slow->next;
slow->next = NULL;
}

root->val = fast->val;