# Simple solution C++ solution

• '''

`````` int length(ListNode*  head){
if(head ==  nullptr) return 0;
int length = 0;
length++;
}
return length;
}

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if(!l2) return l1;
if(!l1) return l2;
int m = length(l1), n = length(l2), diff = abs(m - n), carry = 0;
//If lists are of same length call helper function
if(m == n) {
if(carry == 1){
ListNode* temp = new ListNode(1);
}
}
//Else iterate until we get two lists of same length
ListNode* itr1 = m > n ? l1 : l2; // bigger list
ListNode* itr2 = m < n ? l1 : l2; // smaller list
stack<ListNode*> s;
while(diff > 0){
s.push(itr1);
itr1 = itr1->next;
diff--;
}
//Gives the resulting list from the addition of two lists of equal length

//Stack is used to iterate over the remaining part of the list in reverse order
while(!s.empty()){
int n = s.top()->val + carry;
carry = n/10;
ListNode* temp = new ListNode(n % 10);
temp ->next = head;
s.pop();
}
if(carry == 1){
ListNode* temp = new ListNode(1);
return temp;
}
}

ListNode* addTwoNumbersSameLength(ListNode* l1, ListNode* l2, int* carry){
if(l1 == nullptr) return nullptr;