You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list.
l1 = [2,4,3], l2 = [5,6,4][7,0,8]l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9][8,9,9,9,0,0,0,1]l1 = [0], l2 = [0][0]dummy node, curr = dummy, carry = 0l1 != null || l2 != null || carry != 0d1 = l1.val (or 0), d2 = l2.val (or 0)sum = d1 + d2 + carry; new carry = sum / 10sum % 10 to result; advance pointersdummy.nextStoring digits in reverse order means the least-significant digit comes first. This lets us add digit-by-digit from left to right — exactly how long addition works — without needing to reverse anything. A carry from one column flows naturally to the next node. If the final carry is 1 (e.g. 999 + 1), we append one more node without any special handling.
Converting the lists to integers can overflow for long lists. Instead, add digit by digit, like on paper.
The lowest digit comes first, which is exactly where addition starts. Add the two digits plus the carry, write the sum mod 10, and carry the rest. Keep going while either list or the carry remains.
Reversed digits mean you can add from the head. Don't forget a final carry node.
Run the animation above on your own input, or watch the 1:09 video explanation of Add Two Numbers with a dry run and Java code.