Can someone please help....


  • 0
    M

    Hi,
    I could not figure out what is going on...
    When I run my code, everything is good but when I tried to summit the code.
    I get the following error message.

    Runtime Error Message:
    member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment
    Last executed input:
    [4,5,2,2,9,3,8,9,2]
    [0,7,6,1,6,5,0,6,7]

    Here is my code
    '''
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *c1 = l1, *c2 = l2;
    struct ListNode *l3 = NULL, *c3 = NULL;
    int sum = 0;
    while (c1 != NULL || c2 != NULL) {
    sum /= 10;
    sum += (c1)? c1->val:0;
    sum += (c2)? c2->val:0;

        struct ListNode *tp = (struct ListNode*)malloc(sizeof(struct ListNode));
        tp->val = sum % 10;
        tp->next = NULL;
        
        if (l3 == NULL)
            l3 = c3 = tp;
        else {
            c3->next = tp;
            c3 = c3->next;
        }
        c1 = (c1==NULL)? c1 : c1->next;
        c2 = (c2==NULL)? c2 : c2->next;
    }
    if (sum/10) {
        struct ListNode *tp = (struct ListNode*)malloc(sizeof(struct ListNode));
        tp->val = 1;
        c3->next = tp;
        c3 = c3->next;
    }
    
    return l3;
    

    }
    '''
    And the result of running the code..
    Your input

    [4,5,,2,2,9,3,8,9,2]
    [0,7,6,1,6,5,0,6,7]

    Your answer
    [4,2,9,3,5,9,8,5,0,1]

    Expected answer
    [4,2,9,3,5,9,8,5,0,1]


Log in to reply
 

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