Clean C++ solution


  • 0
    G
    #define L1 (l1 ? l1->val : 0)
    #define L2 (l2 ? l2->val : 0)
    #define L1_NEXT (l1 ? l1->next : NULL)
    #define L2_NEXT (l2 ? l2->next : NULL)
     
    class Solution {
    public:
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
        {
            ListNode *Sum = NULL;
            ListNode **pSum = ∑
            int Carry = 0;
            
            while(l1 != NULL || l2 != NULL || Carry)
            {
                int V = L1 + L2 + Carry;
                Carry = V/10;
                V %= 10;
                *pSum = new ListNode(V);
                pSum = &((*pSum)->next);
                l1 = L1_NEXT;
                l2 = L2_NEXT;
            }
            
            return Sum;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.