2. Add Two Numbers

Given two non-empty linked lists that represent non-negative integers, with their digits stored in reverse order and each node containing a single digit, your task is to add the two numbers and return the result as a new linked list. Assume that the two numbers do not have any leading zeros, except for the number 0 itself. Example:


Example:-

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
JAVA







public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null || l2 == null) {
            return l1 == null ? l2 : l1;
        }

// Use a boolean value to indicate whether there's a carry-over or not
        boolean carry = false;

        ListNode dummy = new ListNode(0);
        ListNode result = dummy;

        while (l1 != null || l2 != null) {
            int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + (carry ? 1 : 0);

            // Create a new node in the result linked list
            if (sum > 9) {
                carry = true;
                sum = sum - 10;
            } else {
                carry = false;
            }
            dummy.next = new ListNode(sum);

            // Move to the next node in each linked list
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
            dummy = dummy.next;
        }

        // Check if there's a carry-over in the last sum
        if (carry) {
            dummy.next = new ListNode(1);
        }

        return result.next;
    }
} 
C++







public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    if (l1 == null || l2 == null) {
        return l1 == null ? l2 : l1;
    }

    boolean carry = false;
    ListNode dummy = new ListNode(0);
    ListNode result = dummy;

    while (l1 != null || l2 != null) {
        int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + (carry ? 1 : 0);

        if (sum > 9) {
            carry = true;
            sum = sum - 10;
        } else {
            carry = false;
        }

        dummy.next = new ListNode(sum);

        if (l1 != null) {
            l1 = l1.next;
        }
        if (l2 != null) {
            l2 = l2.next;
        }
        dummy = dummy.next;
    }

    if (carry) {
        dummy.next = new ListNode(1);
    }

    return result.next;
}
PYTHON
   






# Loop through the linked lists and add the corresponding nodes
    while l1 or l2 or carry:
        val1 = l1.val if l1 else 0
        val2 = l2.val if l2 else 0
        s = val1 + val2 + carry
        carry, val = divmod(s, 10)
        curr.next = ListNode(val)
        curr = curr.next
        
        # Move to the next node in each linked list
        l1 = l1.next if l1 else None
        l2 = l2.next if l2 else None
        
    # Return the result
    return dummy.next
JAVASCRIPT






var addTwoNumbers = function (l1, l2) {
    const dummy = new ListNode();
    let carry = 0;
    let cur = dummy;
    while (l1 || l2 || carry) {
        const s = (l1?.val || 0) + (l2?.val || 0) + carry;
        carry = Math.floor(s / 10);
        cur.next = new ListNode(s % 10);
        cur = cur.next;
        l1 = l1?.next;
        l2 = l2?.next;
    }
    return dummy.next;
};