/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head1 ListNode类 
 * @param head2 ListNode类 
 * @return ListNode类
 */

struct ListNode* ReverseList(struct ListNode* head ) 
{
    if(head==NULL)return NULL;
    struct ListNode* cur=NULL;
    struct ListNode* pre=head;
    struct ListNode* tmp;
    while(pre!=NULL)
    {
        tmp=pre->next;
        pre->next=cur;
        cur=pre;
        pre=tmp;

    }
    return cur;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) 
{
    struct ListNode* newhead1=ReverseList(head1);
    struct ListNode* newhead2=ReverseList(head2);
    struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
    dummy->val = 0;
    dummy->next = NULL;
    struct ListNode* newlist = dummy;
    struct ListNode* p=newhead1;
    struct ListNode* q=newhead2;
    int carry=0;
    while(p!=NULL||q!=NULL||carry != 0)
    {
        int sum=carry;
        if(p!=NULL)
        {
            sum+=p->val;
            p=p->next;
        }
        if(q!=NULL)
        {
            sum+=q->val;
            q=q->next;
        }
        carry=sum/10;
        sum=sum%10;
        struct ListNode* newcode=(struct ListNode*)malloc(sizeof(struct ListNode));
        newcode->val=sum;
        newcode->next=NULL;
        newlist->next=newcode;
        newlist=newcode;
    }
    struct ListNode* list=ReverseList(dummy->next);
    free(dummy);
    return list;
}