Skip to content

Latest commit

 

History

History
56 lines (44 loc) · 1.24 KB

_2816. Double a Number Represented as a Linked List.md

File metadata and controls

56 lines (44 loc) · 1.24 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, Stack

Acceptance Rate : 61.42 %


Solutions

C

/**
 * 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;
}