Is my solution O(1) space and O(n) time?


  • 0
    Z
    ListNode *temp;
    void reorderList(ListNode* head) {
        temp=head;
        int len1=0,len2=0;
        myrun(head,len1,len2);
        return;
    }
    void myrun(ListNode* head, int& len1, int& len2){
        if(!head) return;
        len1++;
        myrun(head->next,len1,len2);
        if(len2<len1/2){
            len2++;
            head->next=temp->next;
            temp->next=head;
            temp=temp->next->next;
        }
        if(len2==(len1%2==0?len1/2:(len1-1)/2)) temp->next=NULL;
        return;
    }

Log in to reply
 

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