The solution I wrote

My Solution.

ListNode* iA = A;
ListNode* iB = B;
ListNode* dummy = new ListNode(0);
ListNode* C = dummy;
int carry = 0;
while (iA != NULL || iB != NULL)
{
    int valA = 0;
    if (iA != NULL)
    {
        valA = iA->val;
        iA = iA->next;
    }

    int valB = 0;
    if (iB != NULL)
    {
        valB = iB->val;
        iB = iB->next;
    }

    int curSum = valA + valB;
    curSum += carry;
    carry = 0;

    if (curSum >= 10)
    {
        carry = 1;
        curSum = curSum % 10;
    }

    ListNode* newNode = new ListNode(curSum);
    C->next = newNode;
    C = newNode;
}

if (carry > 0)
{
    C->next = new ListNode(carry);
}

return dummy->next;
Click here to start solving coding interview questions