Skip to content

Latest commit

 

History

History
60 lines (47 loc) · 1.18 KB

_369. Plus One Linked List.md

File metadata and controls

60 lines (47 loc) · 1.18 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 04, 2024

Last updated : July 04, 2024


Related Topics : Linked List, Math

Acceptance Rate : 61.14 %


Solutions

C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

int plusOneHelper(struct ListNode* curr) {
    if (curr->next) {
        curr->val += plusOneHelper(curr->next);
    } else {
        curr->val++;
    }
    if (curr->val >= 10) {
        curr->val -= 10;
        return 1;
    }
    return 0;
}


struct ListNode* plusOne(struct ListNode* head){
    int carry = plusOneHelper(head);
    if (carry) {
        struct ListNode* newHead = (struct ListNode*) malloc(sizeof(struct ListNode));
        newHead->val = 1;
        newHead->next = head;
        return newHead;
    }
    return head;
}