You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum 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 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]


Example 2:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [8,0,7]


Example 3:

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


&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.


&nbsp;
Follow up:&nbsp;Could you solve it without reversing the input lists?

class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        def number(head):
            ans = ''
            while head:
                ans+=str(head.val)
                head = head.next
            return int(ans) 
        temp = dummy = ListNode(0)
        for i in str(number(l1) + number(l2)):
            temp.next = ListNode(i)
            temp = temp.next
        return dummy.next
      
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res=new ListNode(0);
        ListNode curr=res;
         l1=reverseLinkedList(l1);
         l2=reverseLinkedList(l2);
        int carry=0;
        while(l1!=null||l2!=null||carry==1)
        {
            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; 
            curr.next= new ListNode(sum % 10); 
            
            curr = curr.next; 
        }
        return reverseLinkedList(res.next);
    }
    public ListNode reverseLinkedList(ListNode head)
    {
        ListNode curr=head;
        ListNode prev=null;
        ListNode next;
        while(curr!=null)
        {
            next=curr.next;
            curr.next=prev;
            prev=curr;
            curr=next;
        }
        return prev;
    }
}
/**
 * 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* reverse(ListNode* l1){
        if(l1==NULL||l1->next==NULL){
            return l1;
        }
        ListNode* ans=reverse(l1->next);
        l1->next->next=l1;
        l1->next=NULL;
        return ans;
    }
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
       ListNode* l11= reverse(l1);
       ListNode* l22= reverse(l2);
        ListNode* dummy=new ListNode(0);
        ListNode* curr=dummy;
        int carry=0;
        int sum=0;
        while(l11||l22||carry!=0){
            int x=l11?l11->val:0;
            int y=l22?l22->val:0;
            sum=x+y+carry;
            carry=sum/10;
            curr->next=new ListNode(sum%10);
            curr=curr->next;
            l11=l11?l11->next:nullptr;
            l22=l22?l22->next:nullptr;
        }
        dummy=dummy->next;
       dummy=reverse(dummy);
        return dummy;
    }
};
var addTwoNumbers = function(l1, l2) {
    const reverse = head =>{
        let prev = null
        let dummy = head

        while(dummy){
            let temp = dummy.next
            dummy.next = prev
            prev = dummy
            dummy = temp
        }
        return prev
    }

    let head1 = reverse(l1)
    let head2 = reverse(l2)

    // add
    let sum = new ListNode()
    let p = sum
    let carry = 0

    while((head1 && head2) || carry){
        let v1 = head1?head1.val:0
        let v2 = head2?head2.val:0
        let v = v1+v2+carry

        if(v>=10) carry = 1
        else carry = 0

        let node = new ListNode(v%10)

        sum.next = node
        sum = sum.next
        head1 = head1&&head1.next
        head2 = head2&&head2.next

    }
    sum.next = head1||head2
    return reverse(p.next)
};
Add Two Numbers II