recursive C++


  • 0
    P
    class Solution {
    public:
        int carry = 0;
        ListNode* plusOne(ListNode* head) {
            rec(head);
            if(carry){ListNode *x = new ListNode(carry); x -> next = head; head = x;}
            return head;
            
        }
        int rec(ListNode* head)
        {
            if(!head) return 1;
            int o = rec(head -> next);
            head -> val = head -> val + o + carry;
            carry = head -> val / 10;
            head -> val = head -> val % 10;
            return 0;
        }
    };
    

Log in to reply
 

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