class Solution {
public:

    ListNode* reverseList(ListNode* head)
     {
        ListNode* newNode = new ListNode(0);
        ListNode* cut = head;
        while(cut)
        {
            ListNode* next = cut->next;
            cut->next = newNode->next;
            newNode->next = cut;
            cut = next;
        }
        cut = newNode->next;
        delete newNode; 
        return cut;
    }
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        ListNode* cut = reverseList(head1);
        ListNode* cut1 = reverseList(head2);
        ListNode* ret = new ListNode(0);
        int t = 0;
        while(cut || cut1 || t )
        {
            if(cut)
            {
                t += cut->val;
                cut = cut->next;
            }
            if(cut1)
            {
                t += cut1->val;
                cut1 = cut1->next;
            }
              ListNode* a = ret->next;
             ret->next = new ListNode(t%10);
                ret->next->next = a;
            t /= 10;
        }
        ListNode* b = ret->next;
        delete ret;
        return b;
    }
};