/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 *  ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
#include <cstddef>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param l1 ListNode类
     * @param l2 ListNode类
     * @return ListNode类
     */

    ListNode* ListAdd(ListNode* l1, ListNode* l2) {
        // write code here
        ListNode* prev = new ListNode(-1);
        ListNode* p = prev;
        ListNode* a = l1;
        ListNode* b = l2;
        int m = 0;
        while (a != NULL || b != NULL) {
            if (a != NULL && b != NULL) {
                int sum = a->val + b->val + m;
                com(sum,p,m);
                a = a->next;
                b = b->next;
            } else if (a != NULL && b == NULL) {
                int sum = a->val + m;
                com(sum,p,m);
                a = a->next;
            } else {
                int sum = b->val + m;
                com(sum,p,m);
                b = b->next;
            }
        }
        if (m) {
            ListNode* t = new ListNode(m);
            p->next = t;
        }
        return prev->next;
    }
    void com(int &sum,ListNode* &p,int &m) {
        m = sum / 10;
        sum = sum % 10;
        ListNode* t = new ListNode(sum);
        p->next = t;
        p = p->next;
    }
};