aboutsummaryrefslogtreecommitdiff
path: root/challenge-129/lubos-kolouch/java/ch-2.java
blob: 53a0798f942cc9f001183f72d04428e66b19e4da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;

class Main {
  static class LinkedList {
    int value;
    LinkedList next;

    LinkedList(int value, LinkedList next) {
      this.value = value;
      this.next = next;
    }

    LinkedList add(LinkedList other) {
      int carry = 0;
      LinkedList head = new LinkedList(0, null), tail = head;
      while (this != null || other != null || carry != 0) {
        int sum = (this != null ? this.value : 0) +
                  (other != null ? other.value : 0) + carry;
        tail.next = new LinkedList(sum % 10, null);
        tail = tail.next;
        carry = sum / 10;
        this = this != null ? this.next : null;
        other = other != null ? other.next : null;
      }
      return head.next;
    }

    @Override
    public String toString() {
      LinkedList node = this;
      List<String> values = new ArrayList<>();
      while (node != null) {
        values.add(Integer.toString(node.value));
        node = node.next;
      }
      return String.join(" -> ", values);
    }
  }

  public static void main(String[] args) {
    LinkedList l1 =
        new LinkedList(1, new LinkedList(2, new LinkedList(3, null)));
    LinkedList l2 =
        new LinkedList(3, new LinkedList(2, new LinkedList(1, null)));

    LinkedList result = l1.add(l2);
    System.out.println(result);
  }
}