力扣递归问题
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
int sum1=0,sum2=0,n=0,m=0,sum=0;
while(l1!=NULL)
{
sum1=sum1+(l1->val)*pow(10,n);
l1=l1->next;
n++;
}
while(l2!=NULL)
{
sum2=sum2+(l2->val)*pow(10,m);
l2=l2->next;
m++;
}
sum=sum1+sum2;
if(sum==0) // 特判
{
struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->val = 0;
head->next = NULL;
return head;
}
int q=0;
struct ListNode* p=NULL,*w=NULL,*l=NULL;
while(sum)
{
l=(struct ListNode*)malloc(sizeof(struct ListNode));
l->val=sum%10;
l->next=NULL;
if(q==0)
{
p=l;
}
else
{
w->next=l;
}
q++;
w=l;
sum/=10;
}
return p;
}
为何报错,按这种应该时所有的情况都考虑到了呀?