static const auto io_sync_off=[]()
{
    //turn off sync
    std::ios::sync_with_stdio(false);
    //untie in/out stream;
    std::cin.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        int a = 0, b = 0; //记录链表长度
        head1 = reverse(head1, a); //翻转链表1并返回头
        head2 = reverse(head2, b); //翻转链表2并返回头
        if(a < b) swap(head1, head2); //让较长的链表作为链表1
        ListNode* record = head1; //记录链表1的头,用于后续再次翻转
        bool carry = 0; //记录进位信息
        while(head2) { //因为链表2短,因此只需先遍历链表2
            int temp = head1->val + head2->val + carry; //各位及进位相加
            carry = 0; //进位符清零
            if(temp >= 10) { //若进位
                carry = 1; //设置进位符
                temp %= 10; //取余
            }
            head1->val = temp; //直接在链表1上修改值
            head2 = head2->next; //链表2向前
            if(head2) //判断一下链表2是否遍历完
                head1 = head1->next; //如果没遍历完,则链表1肯定也没遍历完,这样不至于让head1成为nullptr方便后续操作
        }
        while(carry) { //接着在链表1上处理进位
            carry = 0; //进位符清零
            if(head1->next) { //先判断有没有下一位
                head1 = head1->next; //有的话移到下一位
                if(head1->val == 9) { //直接判断是不是9
                    carry = 1; //是9则进位
                    head1->val = 0; //并且设置为0
                }
                else head1->val ++; //否则自身加1
            }
            else head1->next = new ListNode(1); //若无下一位创造值为1的新ListNode
        }
        return reverse(record, a); //返回翻转链表1的头
    }
    ListNode* reverse(ListNode* ln, int &length) { //翻转链表懂的都懂,引用length记录长度
        ListNode* pre = nullptr;
        ListNode* cur = ln;
        ListNode* next = nullptr;
        while(cur) {
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
            length++;
        }
        return pre;
    }
};