# 2. Add Two Numbers - CPP - Solution

• ``````// 2. Add Two Numbers
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *result = NULL;
ListNode *current = NULL;
int carry = 0;
while (l1 || l2 || carry) {
if (l1 && l2) {
int val = carry + l1->val + l2->val;
carry = val / 10;
val %= 10;
ListNode *new_ListNode = new ListNode(val);
if (current == NULL) {
result = new_ListNode;
current = new_ListNode;
l1 = l1->next;
l2 = l2->next;
continue;
}
if (current != NULL) {
current->next = new_ListNode;
current = current->next;
l1 = l1->next;
l2 = l2->next;
continue;
}
l1 = l1->next;
l2 = l2->next;
continue;
}
if (!l1 && !l2 && carry) {
ListNode *new_ListNode = new ListNode(carry);
carry = 0;
current->next = new_ListNode;
return result;
}
if (l1 || l2) {
ListNode *l3 = l1 ? l1 : l2;
int val = carry + l3->val;
carry = val / 10;
val %= 10;
ListNode *new_ListNode = new ListNode(val);
if (current == NULL) {
result = new_ListNode;
current = new_ListNode;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
continue;
}
if (current != NULL) {
current->next = new_ListNode;
current = current->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
continue;
}
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
continue;
}
}
return result;
}
};
int main(void) {
Solution solution;
ListNode *l1 = NULL;
l1 = new ListNode(2);
l1->next = new ListNode(4);
l1->next->next = new ListNode(3);
ListNode *l2 = NULL;
l2 = new ListNode(5);
l2->next = new ListNode(6);
l2->next->next = new ListNode(4);