# Divide&Conquer Java solution Complexity?

• The question is what is time complexity?

``````public class Solution {

while(fast!=null&&fast.next!=null&&fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
}

TreeNode root = new TreeNode(slow.next.val);

ListNode second=slow.next.next;
slow.next=null;

root.right = sortedListToBST(second);

return root;
}
}``````

• Nice idea! I don't know the time complexity either.

• On each recursion level you iterate through entire list 1.5 times with pointers slow and fast. You roughly half the problem size on each recursion call, so recursion depth must be ln(n). So time complexity is n*ln(n). Correct me if I'm wrong.

• I think we can write a recursive equation. T(n) = 2T(n/2) + n, then using master theorem to solve this problem. I think it is O(nlogn).

• sounds logical. I'll vote for you.

• why you set fast as head.next, should fast/slow both start from head to save next.next call.

• Can anyone tell me the space complexity??

• Since we end up creating as many tree nodes as there are in the list, I think the space complexity is O(n). Because space complexity is defined as maximum space the program would take in worst case. Correct me if I am wrong.
But since the depth is logn, I am not sure if the space is O(nlogn) or O(n). Can someone confirm once.
Thanks.

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