You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum&nbsp;as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

&nbsp;
Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.


Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]


Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]


&nbsp;
Constraints:


	The number of nodes in each linked list is in the range [1, 100].
	0 &lt;= Node.val &lt;= 9
	It is guaranteed that the list represents a number that does not have leading zeros.


class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        dummyHead = ListNode(0)
        tail = dummyHead
        carry = 0

        while l1 is not None or l2 is not None or carry != 0:
            digit1 = l1.val if l1 is not None else 0
            digit2 = l2.val if l2 is not None else 0

            sum = digit1 + digit2 + carry
            digit = sum % 10
            carry = sum // 10

            newNode = ListNode(digit)
            tail.next = newNode
            tail = tail.next

            l1 = l1.next if l1 is not None else None
            l2 = l2.next if l2 is not None else None

        result = dummyHead.next
        dummyHead.next = None
        return result
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1 == null) return l2;
        if(l2 == null) return l1;
        
        ListNode dummy = new ListNode(-1);
        ListNode temp = dummy;
        int carry = 0;
        while(l1 != null || l2 != null || carry != 0){
            int sum = 0;
            if(l1 != null){
                sum += l1.val;
                l1 = l1.next;
            }
            if(l2 != null){
                sum += l2.val;
                l2 = l2.next;
            } 
            sum += carry;
            carry = sum / 10;
            ListNode node = new ListNode(sum % 10);
            temp.next = node;
            temp = temp.next;
        }
        return dummy.next;
    }
}
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *head1, *head2, *head3, *temp;
        head1=l1;
        head2 = l2;
        head3=NULL;
        int carry = 0, res = 0;
        while(head1 && head2){
            res = head1->val +head2->val + carry;
            carry = res/10;
            head1->val = res%10;
            if(!head3){
                head3 = head1;
                temp = head3;
            }
            else{
                temp->next = head1;
                temp = temp->next;
            }
            head1 = head1->next;
            head2 = head2->next;
        }
        while(head1){
            res = head1->val + carry;
            carry = res/10;
            head1->val = res%10;
            temp->next = head1;
            temp = temp->next;
            head1 = head1->next;
        }
        while(head2){
            res = head2->val + carry;
            carry = res/10;
            head2->val = res%10;
            temp->next = head2;
            temp = temp->next;
            head2 = head2->next;
        }
        if(carry){
            ListNode* result = new ListNode();
            result->val = carry;
            result->next = NULL;
            temp->next = result;
            temp = temp->next;
        }
        return head3;
    }
};
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
    var List = new ListNode(0);
    var head = List;
    var sum = 0;
    var carry = 0;

    while(l1!==null||l2!==null||sum>0){

        if(l1!==null){
            sum = sum + l1.val;
            l1 = l1.next;
        }
        if(l2!==null){
            sum = sum + l2.val;
            l2 = l2.next;
        }
        if(sum>=10){
            carry = 1;
            sum = sum - 10;
        }

        head.next = new ListNode(sum);
        head = head.next;

        sum = carry;
        carry = 0;

    }

    return List.next;
};
Add Two Numbers