Very Simple C++ Solution for adding two lists

ListNode* Solution::addTwoNumbers(ListNode* A, ListNode* B) {
ListNode *X=new ListNode(0);
ListNode *C=X;
int carry=0;
while(A || B)
{
    int a=0;
    if(A!=NULL)
    {
        a=A->val;
        A=A->next;
    }
    int b=0;
    if(B!=NULL)
    {
        b=B->val;
        B=B->next;
    }
    int res=(a+b+carry);
    carry=res/10;
    res%=10;
    
    ListNode *n= new ListNode(res);
    C->next=n;
    C=n;
    
}
if(carry)
{
    
    C->next=new ListNode(carry);
}
return X->next;
}
Click here to start solving coding interview questions