|
| 1 | +package misc; |
| 2 | + |
| 3 | +/** |
| 4 | + * Algorithm: |
| 5 | + * 1. Create two linked list which will represent two numbers. |
| 6 | + * 2. Reverse both linked list. |
| 7 | + * 3. Add two node values (Each node is being represented as single digit) starting from heads of two linkedlist. |
| 8 | + * 4. If sum is of above two node values is more than 10, then forward the carry. |
| 9 | + * |
| 10 | + */ |
| 11 | +public class AddTwoNumbersRepresentedByLinkedList { |
| 12 | + |
| 13 | + private static Node head; |
| 14 | + |
| 15 | + private static class Node { |
| 16 | + private int value; |
| 17 | + private Node next; |
| 18 | + |
| 19 | + Node(int value) { |
| 20 | + this.value = value; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public void addToTheLast(Node node) { |
| 25 | + if (head == null) { |
| 26 | + head = node; |
| 27 | + } else { |
| 28 | + Node temp = head; |
| 29 | + while (temp.next != null) |
| 30 | + temp = temp.next; |
| 31 | + |
| 32 | + temp.next = node; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public void printList(Node printNode) { |
| 37 | + Node temp = printNode; |
| 38 | + while (temp != null) { |
| 39 | + System.out.format("%d ", temp.value); |
| 40 | + temp = temp.next; |
| 41 | + } |
| 42 | + System.out.println(); |
| 43 | + } |
| 44 | + |
| 45 | + public static Node reverseLinkedList(Node node) { |
| 46 | + if (node == null || node.next == null) { |
| 47 | + return node; |
| 48 | + } |
| 49 | + Node remaining = reverseLinkedList(node.next); |
| 50 | + node.next.next = node; |
| 51 | + node.next = null; |
| 52 | + return remaining; |
| 53 | + } |
| 54 | + |
| 55 | + // This function will do sum of numbers represented by linked list |
| 56 | + public Node findSumOfNumbers(Node l1, Node l2) { |
| 57 | + int carry = 0; |
| 58 | + Node newHead = null; |
| 59 | + Node tempNodeForIteration = null; |
| 60 | + int sum = 0; |
| 61 | + int firstIter = 0; |
| 62 | + while (l1 != null || l2 != null) { |
| 63 | + firstIter++; |
| 64 | + sum = carry; |
| 65 | + if (l1 != null) { |
| 66 | + sum = sum + l1.value; |
| 67 | + l1 = l1.next; |
| 68 | + } |
| 69 | + if (l2 != null) { |
| 70 | + sum = sum + l2.value; |
| 71 | + l2 = l2.next; |
| 72 | + } |
| 73 | + carry = sum / 10; |
| 74 | + sum = sum % 10; |
| 75 | + // Check if it first node for the result |
| 76 | + if (firstIter == 1) { |
| 77 | + tempNodeForIteration = new Node(sum); |
| 78 | + newHead = tempNodeForIteration; |
| 79 | + } else { |
| 80 | + Node tempSumNode = new Node(sum); |
| 81 | + tempNodeForIteration.next = tempSumNode; |
| 82 | + tempNodeForIteration = tempNodeForIteration.next; |
| 83 | + } |
| 84 | + } |
| 85 | + if (carry != 0) { |
| 86 | + Node tempNode = new Node(carry); |
| 87 | + tempNodeForIteration.next = tempNode; |
| 88 | + } |
| 89 | + return newHead; |
| 90 | + } |
| 91 | + |
| 92 | + public static void main(String[] args) { |
| 93 | + AddTwoNumbersRepresentedByLinkedList list = new AddTwoNumbersRepresentedByLinkedList(); |
| 94 | + |
| 95 | + Node head1 = new Node(5); |
| 96 | + list.addToTheLast(head1); |
| 97 | + list.addToTheLast(new Node(6)); |
| 98 | + list.addToTheLast(new Node(7)); |
| 99 | + list.addToTheLast(new Node(1)); |
| 100 | + list.addToTheLast(new Node(2)); |
| 101 | + System.out.print("Number 1: "); |
| 102 | + list.printList(head1); |
| 103 | + head = null; |
| 104 | + Node head2 = new Node(6); |
| 105 | + list.addToTheLast(head2); |
| 106 | + list.addToTheLast(new Node(3)); |
| 107 | + list.addToTheLast(new Node(5)); |
| 108 | + list.addToTheLast(new Node(9)); |
| 109 | + System.out.print("Number 2: "); |
| 110 | + list.printList(head2); |
| 111 | + head1 = reverseLinkedList(head1); |
| 112 | + head2 = reverseLinkedList(head2); |
| 113 | + |
| 114 | + // function to find sum of two linkedlist represent by number |
| 115 | + Node result = list.findSumOfNumbers(head1, head2); |
| 116 | + result = reverseLinkedList(result); |
| 117 | + System.out.print("Sum: "); |
| 118 | + list.printList(result); |
| 119 | + } |
| 120 | +} |
0 commit comments