</>

2. Add Two Numbers

Medium NeetCode 150
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Linked List
Time
—
Space
—

Standard Solution (C++)

SummaryGiven two non-negative integers stored in reverse order across two linked lists (one digit per node), compute their sum and return it as a new linked list in the same reverse-digit format.

Approach
  • Use a dummy head node so the result list can be built without special-casing the first node.
  • Traverse both lists simultaneously, summing corresponding digits plus any carry from the previous step.
  • Continue until both lists are exhausted and there is no remaining carry — this naturally handles lists of unequal length and a final overflow digit (e.g., 999 + 1 = 1000).
  • Carry propagation is straightforward: carry = sum / 10, digit stored = sum % 10.
#include <cstddef>
using namespace std;

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

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode dummy;               // sentinel; avoids edge-case on first node
        ListNode* tail = &dummy;
        int carry = 0;

        // Continue while there are unprocessed digits or a pending carry
        while (l1 || l2 || carry) {
            int digit = carry;
            if (l1) { digit += l1->val; l1 = l1->next; }
            if (l2) { digit += l2->val; l2 = l2->next; }

            carry = digit / 10;
            tail->next = new ListNode(digit % 10);
            tail = tail->next;
        }

        return dummy.next;
    }
};
Time O(max(M, N)) — each list is traversed at most once, where M and N are their respective lengths. Space O(max(M, N)) — the result list holds at most max(M, N) + 1 nodes.

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

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

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummyNode = new ListNode(0);
        ListNode* current=dummyNode;
        int carry=0;

        while(l1 || l2 || carry){
            int val1 = l1 ? l1->val : 0;
            int val2 = l2 ? l2->val : 0;
            int sum=val1+val2+carry;
            carry=sum/10;
            sum%=10;
            current->next=new ListNode(sum);
            l1= l1 ? l1->next : nullptr;
            l2= l2 ? l2->next : nullptr;
            current=current->next;
        }
    
        ListNode* res=dummyNode->next;
        delete dummyNode;
        return res;
    }
};

// Time: O(max(M,N))
// Space: O(max(M,N))

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern