I'm working on LeetCode problem 2. This is my code so far
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
int firstNumber = 0;
int secondNumber = 0;
struct ListNode* finalNode = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode* tmp = finalNode;
int x;
for(x = 0; l1 != NULL || l2 != NULL; x++)
{
if(l1 != NULL)
{
firstNumber += l1->val * pow(10, x);
l1 = l1->next;
}
if(l2 != NULL)
{
secondNumber += l2->val * pow(10, x);
l2 = l2->next;
}
}
char* finalValString = (char*)calloc(x + 1, sizeof(char));
sprintf(finalValString, "%d", firstNumber + secondNumber);
for(int a = x; a >= 0; a--)
{
tmp->val = finalValString[a] - 48;
tmp = tmp->next;
}
free(tmp);
return finalNode;
}
But when I run this code I always get the error:
Line 32: Char 21: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [solution.c] 0xbebebebebebebebe: note: pointer points here
I'm not sure how to fix this.