@@ -0,0 +1,2333 @@
+package com.sunny.grokkingalgorithms.ctc.c2;%0A%0Aimport com.sunny.grokkingalgorithms.ctc.c2.util.LinkedListUtil;%0Aimport com.sunny.grokkingalgorithms.ctc.c2.util.Node;%0A%0A/**%0A * Created by sundas on 3/15/2018.%0A */%0Apublic class AddTwoNumbers %7B%0A%0A  /*%0A  Add two numbers represented by linked list%0A  7-%3E1-%3E6  + 5-%3E9-%3E2%0A  617 + 295%0A  912%0A  stored as 2-%3E1-%3E9%0A   */%0A%0A  /**%0A   *%0A   * @param numberA%0A   * @param numberB%0A   * @return%0A   */%0A  public static Node addTwoNumbers(Node numberA,Node numberB)%7B%0A    Node sumRoot = null;%0A    int previousCarry = 0;%0A    Node aRunner = numberA;%0A    Node bRunner = numberB;%0A    Node previousDigitNode = null;%0A    /*%0A    Assuming that both the linked lists are of same size%0A     */%0A    while(aRunner != null && bRunner != null)%7B%0A      int tempSum = aRunner.data + bRunner.data + previousCarry;%0A      int currentDigit = tempSum%2510;%0A      previousCarry = tempSum/10;%0A      Node currentdigitNode = new Node();%0A      currentdigitNode.data = currentDigit;%0A      if(sumRoot == null)%7B%0A        sumRoot = currentdigitNode;%0A      %7D%0A      if(previousDigitNode != null)%7B%0A        previousDigitNode.next = currentdigitNode;%0A      %7D%0A      previousDigitNode = currentdigitNode;%0A      aRunner = aRunner.next;%0A      bRunner = bRunner.next;%0A    %7D%0A    if(previousCarry %3E 0)%7B%0A      Node currentDigitNode = new Node();%0A      currentDigitNode.data = previousCarry;%0A      if(previousDigitNode != null)%7B%0A        previousDigitNode.next = currentDigitNode;%0A      %7D%0A    %7D%0A    return sumRoot;%0A  %7D%0A%0A  /**%0A   *%0A   * @param args%0A   */%0A  public static void main(String%5B%5D args) %7B%0A    int%5B%5D input = new int%5B%5D%7B7,1,6%7D;%0A    Node numberA = LinkedListUtil.createLinkedList(input);%0A    LinkedListUtil.printLinkedList(numberA);%0A    input = new int%5B%5D%7B5,9,2%7D;%0A    Node numberB = LinkedListUtil.createLinkedList(input);%0A    LinkedListUtil.printLinkedList(numberB);%0A    Node sumRoot = addTwoNumbers(numberA, numberB);%0A    LinkedListUtil.printLinkedList(sumRoot);%0A    System.out.println(%22next.......%22);%0A    input = new int%5B%5D%7B7,1,9%7D;%0A    numberA = LinkedListUtil.createLinkedList(input);%0A    LinkedListUtil.printLinkedList(numberA);%0A    input = new int%5B%5D%7B5,9,9%7D;%0A    numberB = LinkedListUtil.createLinkedList(input);%0A    LinkedListUtil.printLinkedList(numberB);%0A    sumRoot = addTwoNumbers(numberA,numberB);%0A    LinkedListUtil.printLinkedList(sumRoot);%0A%0A  %7D%0A%0A%7D%0A

Add two numbers represented by linked lists stored in reverse direction.