369. Plus One Linked List
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : July 04, 2024
Last updated : July 04, 2024
Related Topics : Linked List, Math
Acceptance Rate : 61.14 %
/**
* 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;
}