33,311
社区成员
发帖
与我相关
我的任务
分享
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if(l1==NULL||l2==NULL)
return NULL;
ListNode *res=new ListNode((l1->val+l2->val)%10);
ListNode *cur=res;
int flag=0;
if(l1->val+l2->val>=10)
flag+=1;
l1=l1->next;
l2=l2->next;
while(l1&&l2){
int s=l1->val+l2->val+flag;
flag=0;
if(s>=10){
flag++;
s%=10;
}
ListNode *tmp=new ListNode(s);
cur->next=tmp;
cur=cur->next;
l1=l1->next;
l2=l2->next;
}
while(l1){
int a=l1->val+flag;
ListNode *tmp=new ListNode((a)%10);
cur->next=tmp;
cur=cur->next;
flag=0;
if(a>=10)
flag+=1;
l1=l1->next;
}
while(l2){
int a=l2->val+flag;
ListNode *tmp=new ListNode((a)%10);
cur->next=tmp;
cur=cur->next;
flag=0;
if(a>=10)
flag+=1;
l2=l2->next;
}
if(flag){
ListNode *tmp=new ListNode(flag);
cur->next=tmp;
}
return res;
}
curren2t=l2; //注意这儿多了一个t,是current2后面多了一个t
i=1;
while(current2!=NULL)
{
sum2=sum2+(current2->val)*power(i);// Line 24: dereferencing pointer to incomplete type 'struct listNode'就是这里
current2=current2->next;
i++;
}