4ms c++ solution


  • 1
    C
    class Solution {
    private:
        int compute(ListNode* head){
            ListNode* curr=head;
            if (!curr->next)
                curr->val++;
            else
                curr->val+=compute(head->next);
                
            if (curr->val>=10){
                curr->val-=10;
                return 1;
            }
            return 0;
        }
    public:
        ListNode* plusOne(ListNode* head) {
            if (!head) return head;
            if (compute(head)==1) {
                ListNode* temp = head;
                ListNode* preHead = new ListNode(1);
                preHead->next = temp;
                head = preHead;
            }
            return head;
        }
    };
    

Log in to reply
 

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