保存前面第一个不为9的节点pre,若当前节点和大于9,pre节点值加1,pre之后的节点到当前节点之前的所有节点置0。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        // write code here
        auto len = [](ListNode* t){
            int count = 0;
            while(t){
                count++;
                t = t->next;
            }
            return count;
        };
        int l1 = len(head1),l2 = len(head2);
        if(l1 < l2) {
            swap(head1,head2);
            swap(l1,l2);
        }
        ListNode* res = new ListNode(0),*pre = res;
        res->next = head1;
        l1 -= l2;
        while(head1 && head2){
            int t1 = head1->val;
            int t2 = 0;
            if(l1-- <= 0){
                t2 = head2->val;
                head2 = head2->next;
            }
            if(t1 + t2 < 10)
            {
                head1->val = t1+t2;
                if(head1->val != 9) pre = head1;
            }else{
                head1->val = (t1 + t2) % 10;
                pre->val++;pre = pre->next;
                while(pre != head1){
                    pre->val = 0;
                    pre = pre->next;
                }
            }
            head1 = head1->next;
        }
        if(res->val == 0) return res->next;
        return res;
    }
};