C++ recursive 7.25%, can I do better?


  • 0
    F

    I think recursive is meant to only beat 7.25%.
    Here's my recursive code

    ListNode* plusOne(ListNode* head) {
            ListNode* ans=new ListNode(1);
            if(head==NULL)return ans;
            if(rec(head,NULL)==1 ){
                ans->next = head;
                return ans;
            }else{
                //==0
                cout<<"first 0"<<endl;
                return head;
            }
        }
        int rec(ListNode* cur, ListNode* par){
            //return carry;
            if(cur==NULL){
                return 1;
            }
            int carry = rec(cur->next, cur);
            int tmp= (cur->val+carry)/10;
            cur->val = ( cur->val + carry )%10 ;
            return tmp ;
        }
    

Log in to reply
 

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