Skip to content

Commit 187015b

Browse files
committed
Add solution of AddTwoNumbers task
1 parent e829ac5 commit 187015b

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package by.andd3dfx.numeric;
2+
3+
import java.util.Iterator;
4+
import lombok.AllArgsConstructor;
5+
import lombok.EqualsAndHashCode;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* <pre>
10+
* <a href="https://leetcode.com/problems/add-two-numbers/description/">Task description</a>
11+
*
12+
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order,
13+
* and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
14+
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
15+
*
16+
* Example 1:
17+
* Input: l1 = [2,4,3], l2 = [5,6,4]
18+
* Output: [7,0,8]
19+
* Explanation: 342 + 465 = 807.
20+
*
21+
* Example 2:
22+
* Input: l1 = [0], l2 = [0]
23+
* Output: [0]
24+
*
25+
* Example 3:
26+
* Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
27+
* Output: [8,9,9,9,0,0,0,1]
28+
* </pre>
29+
*/
30+
public class AddTwoNumbers {
31+
32+
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
33+
var iterator1 = new DigitsIterator(l1);
34+
var iterator2 = new DigitsIterator(l2);
35+
36+
var res = new ListNode();
37+
var accumulator = 0;
38+
var curr = res;
39+
while (iterator1.hasNext() || iterator2.hasNext() || accumulator > 0) {
40+
var n1 = iterator1.hasNext() ? iterator1.next() : 0;
41+
var n2 = iterator2.hasNext() ? iterator2.next() : 0;
42+
accumulator = calcAccumulator(n1 + n2 + accumulator, curr);
43+
curr = curr.next;
44+
}
45+
return res.next;
46+
}
47+
48+
private static int calcAccumulator(int sum, ListNode curr) {
49+
if (sum <= 9) {
50+
curr.next = new ListNode(sum);
51+
} else {
52+
curr.next = new ListNode(sum % 10);
53+
}
54+
return sum / 10;
55+
}
56+
57+
@EqualsAndHashCode
58+
@NoArgsConstructor
59+
@AllArgsConstructor
60+
public static class ListNode {
61+
62+
int val;
63+
ListNode next;
64+
65+
ListNode(int val) {
66+
this.val = val;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "{%d -> %s}".formatted(val, next);
72+
}
73+
}
74+
75+
public static class DigitsIterator implements Iterator<Integer> {
76+
77+
private ListNode listNode;
78+
79+
public DigitsIterator(ListNode listNode) {
80+
this.listNode = listNode;
81+
}
82+
83+
@Override
84+
public boolean hasNext() {
85+
return listNode != null;
86+
}
87+
88+
@Override
89+
public Integer next() {
90+
var res = listNode.val;
91+
listNode = listNode.next;
92+
return res;
93+
}
94+
}
95+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package by.andd3dfx.numeric;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import by.andd3dfx.numeric.AddTwoNumbers.ListNode;
6+
import org.junit.Test;
7+
8+
public class AddTwoNumbersTest {
9+
10+
@Test
11+
public void addTwoNumbers() {
12+
assertThat(AddTwoNumbers.addTwoNumbers(
13+
new ListNode(2,
14+
new ListNode(4,
15+
new ListNode(3))),
16+
new ListNode(5,
17+
new ListNode(6,
18+
new ListNode(4)))
19+
)).isEqualTo(
20+
new ListNode(7,
21+
new ListNode(0,
22+
new ListNode(8))));
23+
24+
assertThat(AddTwoNumbers.addTwoNumbers(
25+
new ListNode(0),
26+
new ListNode(0)
27+
)).isEqualTo(
28+
new ListNode(0));
29+
30+
assertThat(AddTwoNumbers.addTwoNumbers(
31+
new ListNode(9,
32+
new ListNode(9,
33+
new ListNode(9,
34+
new ListNode(9,
35+
new ListNode(9,
36+
new ListNode(9,
37+
new ListNode(9))))))),
38+
new ListNode(9,
39+
new ListNode(9,
40+
new ListNode(9,
41+
new ListNode(9))))
42+
)).isEqualTo(
43+
new ListNode(8,
44+
new ListNode(9,
45+
new ListNode(9,
46+
new ListNode(9,
47+
new ListNode(0,
48+
new ListNode(0,
49+
new ListNode(0,
50+
new ListNode(1)))))))));
51+
}
52+
}

0 commit comments

Comments
 (0)