|
| 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 | +} |
0 commit comments