Add Two Numbers Stored in Reverse Order
Solve this Probleml1 and l2, each representing a non-negative integer with its digits stored in reverse order — the head node holds the ones digit. Add the two numbers and return the sum as a linked list in the same reverse-order format.
Because the least significant digit already comes first in both lists, this maps almost directly onto how addition works by hand: process one digit position at a time, track a carryCarryWhenever two digits (plus any incoming carry) sum to 10 or more, only the ones digit is kept at that position — the tens digit "carries" forward to be added into the next position over. for anything that overflows past 9, and keep going until both lists — and the carry — are exhausted.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ number of nodes in l1, l2 ≤ 100 - ◆
0 ≤ node value ≤ 9 - ◆
Each list represents a non-negative integer with digits stored in reverse order — the head is the ones digit — and has no leading zeros, except the number 0 itself
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public Node addTwoNumbers(Node l1, Node l2) { |
| 3 | Node dummy = new Node(0); |
| 4 | Node tail = dummy; |
| 5 | int carry = 0; |
| 6 | while (l1 != null || l2 != null || carry > 0) { |
| 7 | int x = (l1 != null) ? l1.val : 0; |
| 8 | int y = (l2 != null) ? l2.val : 0; |
| 9 | int sum = x + y + carry; |
| 10 | carry = sum / 10; |
| 11 | tail.next = new Node(sum % 10); |
| 12 | tail = tail.next; |
| 13 | if (l1 != null) l1 = l1.next; |
| 14 | if (l2 != null) l2 = l2.next; |
| 15 | } |
| 16 | return dummy.next; |
| 17 | } |
| 18 | } |
| 19 |
dummy0A dummy node lets the first digit attach the same way as every later one. carry starts at 0.
Approach & Solutions
Brute Force — Convert Each List to a Number, Add, Rebuild the Result
GoodSince each list already stores its digits least-significant-first, walk each list once, multiplying by an increasing power of 10 to reconstruct the actual number it represents. Add the two numbers normally, then peel the sum apart one digit at a time (also least-significant-first, via % 10 and / 10) to build the result list — which conveniently comes out in the same reverse order the problem wants, with no extra reversal needed.
O(n + m)O(1)1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node addTwoNumbers(Node l1, Node l2) {
9 long num1 = 0, mult1 = 1;
10 while (l1 != null) {
11 num1 += l1.val * mult1;
12 mult1 *= 10;
13 l1 = l1.next;
14 }
15 long num2 = 0, mult2 = 1;
16 while (l2 != null) {
17 num2 += l2.val * mult2;
18 mult2 *= 10;
19 l2 = l2.next;
20 }
21 long sum = num1 + num2;
22 Node dummy = new Node(0);
23 Node tail = dummy;
24 if (sum == 0) {
25 tail.next = new Node(0);
26 tail = tail.next;
27 }
28 while (sum > 0) {
29 tail.next = new Node((int) (sum % 10));
30 tail = tail.next;
31 sum /= 10;
32 }
33 return dummy.next;
34 }
35}Optimal — Single Pass With Carry, Build the Result as You Go
OptimalWalk l1 and l2 side by side, one node at a time. At each step, add whichever values exist (0 if a list has run out) plus the carry from last time, and append a single new digit node to the result. Keep going as long as either list still has nodes, or a carry is still pending. No number ever gets reconstructed — the addition happens node by node, exactly like adding by hand.
O(max(n, m))O(max(n, m))1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node addTwoNumbers(Node l1, Node l2) {
9 Node dummy = new Node(0);
10 Node tail = dummy;
11 int carry = 0;
12 while (l1 != null || l2 != null || carry > 0) {
13 int x = (l1 != null) ? l1.val : 0;
14 int y = (l2 != null) ? l2.val : 0;
15 int sum = x + y + carry;
16 carry = sum / 10;
17 tail.next = new Node(sum % 10);
18 tail = tail.next;
19 if (l1 != null) l1 = l1.next;
20 if (l2 != null) l2 = l2.next;
21 }
22 return dummy.next;
23 }
24}