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, Stack
Acceptance Rate : 61.42 %
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int doubleItHelper(struct ListNode* curr) {
curr->val *= 2;
if (curr->next) {
curr->val += doubleItHelper(curr->next);
}
int carry = curr->val / 10;
curr->val %= 10;
return carry;
}
struct ListNode* doubleIt(struct ListNode* head){
int carry = doubleItHelper(head);
if (carry) {
struct ListNode* newHead = (struct ListNode*) malloc(sizeof(struct ListNode));
newHead->val = carry;
newHead->next = head;
return newHead;
}
return head;
}