class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int i=1;
int k1=0,k2=0,k3=0;
ListNode *l3,*pointer;
while(l1>next!=NULL){
l1=l1>next;
k1=(l1>val)*i+k1;
i*=10;
}
i=1;
while(l2>next!=NULL){
l2=l2>next;
k2=(l2>val)*i+k2;
i*=10;
}
if(k1==0) return l2;
if(k2==0) return l1;
k3=k1+k2;
pointer=l3;
while(k3!=0){
pointer=new ListNode(k3%10);
pointer=pointer>next;
k3/=10;
}
return l3;
}
};
What is the mistake,please tell me(the code is ugly but just tell why it can not be accepted)


Just as
@ nbauerlevey
has said, you can't convert each list into integers and add two numbers. If the integers is very big, thek1
andk2
will be overflow, ork1 + k2
. You may think usinglong long
insteadint
, but its not a good way to solve this question. For this problem, we should scanning two lists and doing sum operations each node.