Я понял проблему, но не могу решить ее в «C».
struct ListNode* newnode(struct ListNode* node, int data) {
struct ListNode* temp = node;
struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newnode;
newnode->next = NULL;
newnode->val = data;
return newnode;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* l;
struct ListNode* temp = l;
int carry = 0;
while (l1 != NULL || l2 != NULL) {
int sum = 0;
if (l1 != NULL) {
sum += l1->val;
l1 = l1->next;
}
if (l2 != NULL) {
sum += l2->val;
l2 = l2->next;
}
if (sum >= 10) {
temp = newnode(l, carry);
carry = sum % 10;
}
else {
temp = newnode(l, carry + sum);
}
}
return l;
}
Получение ошибки времени выполнения: доступ к элементу в нулевом указателе типа «struct ListNode» (solution.c).Я запустил функцию 'newnode' отдельно, которая отлично работает.
Нужна помощь, только новичок.