Placement

Leet Code : Add Two List – Java | CPP | Javascript | Python

You are given two non-empty linked lists that each represent a non-negative integer. The digits are kept in reverse order, with each node containing only one digit. Add the two numbers together and return the total as a linked list.

Except for the number 0 itself, you may presume that the two numbers do not have any leading zeros.

Eg 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]

Solution:

CPP :

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* head=NULL;
        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);
            // to add the first node in the newly created list (head node)
            if(head==NULL)
            {
                head=node;
                continue;  //forwards the loop for further itreation before moving further
            }
            
            // to add the remaining list except the head node
            ListNode* temp=head;
            while(temp->next!=NULL)
            {
                temp=temp->next;
            }
            temp->next=node;
        }
        return head;
    }
};

Java

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int count = 1;
        int carry = 0;
        ListNode resultNode = new ListNode();
        ListNode nextNode = resultNode;
        while(l1 != null || l2 != null || carry != 0) {
            int sum = carry;
            if(l1 != null){
               sum += l1.val;
                l1 = l1.next;
            } 
                
            if(l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            } 
            
            if(sum >9) {
                carry = 1;
                nextNode.val = sum - 10;
            } else {
                carry = 0;
                nextNode.val = sum;
            }
            if(l1 != null || l2 != null || carry != 0) {
                nextNode.next = new ListNode();
                nextNode = nextNode.next;
            }
        }
        return resultNode;
    }
}

Python :

class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        head=ListNode()
        
        tail=ListNode()
        
        a=list()
        b=list()
        while(l1!=None):
            a.append(l1.val)
            l1=l1.next
        while(l2!=None):
            b.append(l2.val)
            l2=l2.next
        a.reverse()
        b.reverse()
        print(a)
        print(b)
        res=list()
        carry=0
        if(len(a)>=len(b)):
            i=len(a)-1
            j=len(b)-1
            while(i>=0):
                if j >=0:
                    s=a[i]+b[j]+carry
                else:
                    s=a[i]+carry
                if (s<10):
                    res.append(s)
                    carry=0
                else:
                    res.append(s%10)
                    s=s//10
                    carry=s
                i-=1  
                j-=1
            if carry!=0:
                res.append(carry)
            print(res)
            
            for i in range (0,len(res)):
                temp=ListNode()
               
                temp.val=res[i]
                # print(temp.val)
                temp.next=None
                
                if i==0:
                    head=temp
                    tail=temp
                else:
                    tail.next=temp
                    tail=temp
                print(tail.val)
            # print(head)
            
        elif(len(b)>=len(a)):
            res=list()
            carry=0
            i=len(b)-1
            j=len(a)-1
            while(i>=0):
                if j >=0:
                    s=a[j]+b[i]+carry
                else:
                    s=b[i]+carry
                if (s<10):
                    res.append(s)
                    carry=0
                else:
                    res.append(s%10)
                    s=s//10
                    carry=s
                i-=1  
                j-=1
            if carry!=0:
                res.append(carry)
            print(res)
            
            for i in range (0,len(res)):
                temp=ListNode()
               
                temp.val=res[i]
                # print(temp.val)
                temp.next=None
                
                if i==0:
                    head=temp
                    tail=temp
                else:
                    tail.next=temp
                    tail=temp
                print(tail.val)
        return head

Java Script:

var addTwoNumbers = function(l1, l2) {
    const zeroNode = new ListNode();
    let l3 = zeroNode;
    let carry = 0;
    
    while (l1 || l2) {
        const value1 = l1?.val || 0;
        const value2 = l2?.val || 0;
        const sum = value1 + value2 + carry;
        const value3 = sum % 10;
        
        carry = Math.floor(sum / 10);
        
        l1 = l1 ? l1.next : null;
        l2 = l2 ? l2.next : null;
        l3.next = new ListNode(value3);
        l3 = l3.next;
    }
    
    if (carry) {
        l3.next = new ListNode(carry);
    }
    
    return zeroNode.next;
};
Facebook
Twitter
LinkedIn

Leave a Comment