你的函数有几个严重的问题。
例如,如果传递给函数的指针都是空指针,则函数返回一个指向未初始化节点的指针
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode * head1=l1;
struct ListNode * head2=l2;
struct ListNode *toReturn=malloc(sizeof(struct ListNode));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//it is complainig about this temp
struct ListNode *temp = NULL;
int carry,total=0;
while(head1 !=NULL || head2!=NULL)
{
//...
}
return toReturn;
}
所以这个内存分配在函数的开头
struct ListNode *toReturn=malloc(sizeof(struct ListNode));
没有意义。
如果指针head1 或head2 之一等于NULL,则此while 循环
while(head1 !=NULL || head2!=NULL)
{
//...
}
调用未定义的行为,因为在循环中您正在使用空指针访问内存,例如在此语句中
total = head1->val + head2->val;
你应该在while循环中写下条件
while(head1 !=NULL && head2!=NULL)
{
//...
}
创建列表的最后一个节点也未初始化
while(head1 !=NULL || head2!=NULL)
{
temp = malloc(sizeof(struct ListNode));
//...
toReturn-> val = total;
toReturn->next = temp;
head1= head1->next;
head2=head2->next;
}
而且你总是在改变指针toReturn指向的同一个节点
toReturn-> val = total;
toReturn->next = temp;
所以函数产生了内存泄漏并且列表没有被构建。
许多 if 语句是多余的。例如,而不是
if(carry==0)
{
total = head1->val + head2->val;
}
else if(carry!=0)
{
total = head1->val + head2->val+carry;
}
你可以只写一个语句
total = head1->val + head2->val+carry;
当head1 或head2 等于NULL 时,您需要为函数附加一个处理其中一个列表的代码。
如果您认为列表将包含无符号数字,则结构 ListNode 的数据成员 val 的类型也应为 unsigned int,否则您还需要存储已存储数字的符号。
这是一个演示程序,展示了如何编写函数addTwoNumbers。
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
unsigned int val;
struct ListNode *next;
};
int set( struct ListNode **head, unsigned long long n )
{
const unsigned int Base = 10;
while ( *head )
{
struct ListNode *tmp = *head;
head = &( *head )->next;
free( tmp );
}
int success = 1;
struct ListNode **current = head;
do
{
*current = malloc( sizeof( struct ListNode ) );
success = *current != NULL;
if ( success )
{
( *current )->val = n % Base;
( *current )->next = NULL;
current = &( *current )->next;
}
} while ( success && ( n /= Base ) );
if ( !success )
{
while ( *head )
{
struct ListNode *tmp = *head;
head = &( *head )->next;
free( tmp );
}
}
return success;
}
FILE * display( const struct ListNode *head, FILE *fp )
{
if ( head )
{
display( head->next, fp );
fprintf( fp, "%u", head->val );
}
return fp;
}
struct ListNode * addTwoNumbers( const struct ListNode *head1,
const struct ListNode *head2 )
{
const unsigned Base = 10;
struct ListNode *result = NULL;
struct ListNode **current = &result;
unsigned int carry = 0;
int success = 1;
while ( success && ( head1 || head2 ) )
{
*current = malloc( sizeof( struct ListNode ) );
success = *current != NULL;
if ( success )
{
( *current )->next = NULL;
unsigned int sum = carry + ( head1 ? head1->val : 0 ) +
( head2 ? head2->val : 0 );
( *current )->val = sum % Base;
carry = !( sum < Base );
if ( head1 ) head1 = head1->next;
if ( head2 ) head2 = head2->next;
current = &( *current )->next;
}
}
if ( !success )
{
while ( result )
{
struct ListNode *tmp = result;
result = result->next;
free( tmp );
}
}
return result;
}
int main(void)
{
struct ListNode *head1 = NULL;
set( &head1, 123456789 );
fputc( '\n', display( head1, stdout ) );
struct ListNode *head2 = NULL;
set( &head2, 123456789 );
fputc( '\n', display( head2, stdout ) );
struct ListNode *sum = addTwoNumbers( head1, head2 );
fputc( '\n', display( sum, stdout ) );
return 0;
}
程序输出是
123456789
123456789
246913578
如果您希望列表也可以包含负数,则列表的第一个节点可以包含符号:0 - 数字是非负数,1 - 数字是负数。另一种方法是将带符号的数字存储在列表的节点中。