2020-08-16-链表

Record

Posted by Jocelyn on August 16, 2020

2020-08-16 recording.

class Solution2 {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int currentSum = 0;
        int carryDigit = 0;
        ListNode *resTmp = new ListNode(0);//哑节点
        ListNode *res = resTmp;
        
        while(l1||l2||carryDigit)
        {
            int sumTmp = 0;
            if(l1)
            {
                sumTmp +=l1->val;
                l1 = l1->next;
            }
            if(l2)
            {
                sumTmp+=l2->val;
                l2 = l2->next;
            }
            sumTmp+=carryDigit;
            currentSum = sumTmp%10;
            carryDigit = sumTmp/10;
            
            ListNode *currentNode = new ListNode(currentSum);
            res->next = currentNode;
            res = res->next;
            
        }
        res = resTmp->next;//切断 删掉
        delete resTmp;
        return res;
    }
};

委屈。。。需要KFC鸡肉卷的安慰才能站起来~~~