add two interviewbit
add two interviewbit
{
if(A==NULL||A->next == NULL)
{
return A;
}
ListNode *t = reverse(A->next);
A->next->next = A;
A->next = NULL;
return t;
}
ListNode *addall(ListNode *A,ListNode *B)
{
ListNode *res = NULL;
ListNode *temp,*prev=NULL;
int c=0,sum;
while(A!=NULL||B!=NULL)
{
sum = c+(A?A->val:0)+(B?B->val:0);
c = (sum>=10)?1:0;
sum = sum%10;
temp = new ListNode(sum);
if(res == NULL)
{
res = temp;
}
else
{
prev->next = temp;
}
prev = temp;
if(A)
{
A=A->next;
}
if(B)
{
B=B->next;
}
}
if(c>0)
temp->next = new ListNode(c);
return res;
}
void print(ListNode*h)
{
while(h!=NULL)
{
cout<<h->val<<" ";
h = h->next;
}
cout<<"\n";
}
ListNode* Solution::addTwoNumbers(ListNode* A, ListNode* B) {
print(reverse(addall(reverse(A),reverse(B))));
}