Simple Java Solution O(nlogn) 1ms

  • 0
     public TreeNode sortedListToBST(ListNode head) {
                return null;
            ListNode slow = head;
            ListNode fast = head;
            ListNode endMarker = null;
            while(fast!=null &&!=null){
                endMarker = slow;
                slow =;
                fast =;
            TreeNode root = new TreeNode(slow.val);
            if(endMarker!=null) //marking end point of first list
       = null;
                head = null;
            root.left = sortedListToBST(head);
            root.right = sortedListToBST(;
            return root;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.