List multiplication

List multiplication

Algorithm:

  • Base case: if lis1 or list2 is empty then return 0
  • Set following variables:
    • carry =0, skipCounter = 1, multi = 0 and resultHead = null
  • Iterate list2 till node2 != null
    • move resultNode from resultHead by skipCounter steps and
    • Increase skipCounter by one
    • Iterate list1 till node1 != null
      • multi = getPreviousDigit(resultNode) + carry + node2.getData() * node1.getData()
      • add multi % 10 to resultNode
      • carry = multi / 10
    • If carry > 0 then, add to result set
  • Return resultHead

 

Latest Source Code:
Github: ListMultiplication.java

Output:

385 X 85 = 32725
385 X 85 = 39
Author: Hrishikesh Mishra