diff options
Diffstat (limited to 'challenge-129/lubos-kolouch/java/ch-2.java')
| -rw-r--r-- | challenge-129/lubos-kolouch/java/ch-2.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-129/lubos-kolouch/java/ch-2.java b/challenge-129/lubos-kolouch/java/ch-2.java new file mode 100644 index 0000000000..53a0798f94 --- /dev/null +++ b/challenge-129/lubos-kolouch/java/ch-2.java @@ -0,0 +1,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); + } +} |
