# 2. Add Two Numbers - CPP - Solution

• ``````//2. Add Two Numbers
#include <iostream>
#include <algorithm>
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) {
int l1_len = length(l1);
int l2_len = length(l2);
int ret_len = max(l1_len, l2_len);
}
private:
int length(ListNode* l) {
int len = 0;
ListNode* itr = l;
while (itr) {
++len;
itr = itr->next;
}
return len;
}
private:
ListNode* initialize_list(int len) {
if (len < 1) {
exit(0);
}
else if (len >= 1) {
int l = 0;
while (l < len - 1) {
itr->next = new ListNode(0);
itr = itr->next;
++l;
}
}
}
private:
void plus_list(ListNode* ret_head, ListNode* l1, ListNode* l2) {
ListNode* l1_itr = l1;
ListNode* l2_itr = l2;
if (l1_itr && l2_itr) {
l1_itr = l1_itr->next;
l2_itr = l2_itr->next;
}
else if (l1_itr && !l2_itr) {
l1_itr = l1_itr->next;
}
else if (!l1_itr && l2_itr) {
l2_itr = l2_itr->next;
}
}
}
private:
while (itr) {
if (itr->val >= 10 && itr->next) {
itr->val -= 10;
itr->next->val += 1;
}
else if (itr->val >= 10 && !itr->next) {
itr->next = new ListNode(0);
itr->val -= 10;
itr->next->val += 1;
}
itr = itr->next;
}
}
};
int main(int argc, char** argv) {
Solution solution;
getchar();
return 0;
}``````

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