Use Lambda to keep code simple


  • 0
    R
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
            int carry = 0;
            auto head = new ListNode(0);
            auto current = head;
            auto get = [](ListNode*& l){
                auto rst = l ? l->val : 0;
                l = l ? l->next : NULL;
                return rst;
            };
            while(l1 || l2 || carry)
            {
                current->next = new ListNode(0);
                current = current->next;
                current->val = get(l1) + get(l2) + carry;
                carry = current->val / 10;
                current->val %= 10;
            }
            return head->next;
        }
    

    Still I don't konw why Java solutions are much faster than c or c++ solutions?


Log in to reply
 

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