咱这个空间是O1吧
class Solution {
public ListNode sortList(ListNode head) {
if(head==null || head.next==null) return head;
//分成两段
ListNode step1=head,step2=head.next;
while(step2!=null && step2.next!=null){
step1=step1.next;
step2=step2.next.next;
}
ListNode h=one.next;
one.next=null;
ListNode sortedLeftHead=sortList(head);
ListNode sortedRightHead=sortList(h);
return merge(sortedLeftHead,sortedRightHead);
}
ListNode merge(ListNode l1,ListNode l2){
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next=merge(l1.next,l2);
return l1;
}else{
l2.next=merge(l1,l2.next);
return l2;
}
}
}
代码里面突然出现了 one ????
栈占空间吧
有道理==