title: Adding Two num­bers rep­re­sented by a linked list, Num­ber Stored in FORWARD order- Python
body: <p>I am trying to add two numbers represented by Linked List in <strong>FORWARD</strong> direction. I have written the function to add them, but I am unable to understand how to forward carry to next level. It is turning out to be zero all time. Please advise me corrections in add function below,</p>
  
  <p><strong>Update1#</strong>  I have written padding functionto make linked lists of equal length by adding zero to front of smaller number as below,</p>
  
  <pre><code>**Input:**
  First Number     : 351            0-&gt;3-&gt;5-&gt;1
  Second Number    : 2249           2-&gt;2-&gt;4-&gt;9 
  Correct Addition : 2600           2-&gt;6-&gt;0-&gt;0
  The result I am getting is 2590   2-&gt;5-&gt;9-&gt;0
  </code></pre>
  
  <p>It is doing the addition correctly, but not able to forward carry to next level.</p>
  
  <pre><code>=======================  Code   ============================
  #The carry is passed as zero.
  def addRecursively(self, h1, h2, carry):
      if h1 == None and h2 == None:
          return None
      self.addRecursively( h1.next, h2.next, carry)
  
      print "h1.val: ", h1.data," h2.val: ", h2.data, " Carry: ", carry
      num =carry
      if h1:
          num +=h1.data
      if h2:
          num +=h2.data
      carry = 1 if num&gt;=10 else 0
      num = num % 10
      print "num: ", num, " carry: ",carry
  
     =============================================================
     Output:
     h1.val:  1  h2.val:  9  Carry:  0
     num:  0  carry:  1
     h1.val:  5  h2.val:  4  Carry:  0
     num:  9  carry:  0
     h1.val:  3  h2.val:  2  Carry:  0
     num:  5  carry:  0
     h1.val:  0  h2.val:  2  Carry:  0
     num:  2  carry:  0
  </code></pre>
  
  <p><strong>Update#2:</strong></p>
  
  <pre><code>    def saveToLL(self, data) :
          if self.head == None:
              self.head = Node(data)
          else:
              node = Node(data)
              node.next = self.head
              self.head =node
          return self.head
  #    =====  Addition function to add element using stack  =====          
  
      def addRecursively(self, h1, h2):
          if h1 == None and h2 == None:
              return 0
  # carry will be returned by recursive call
          carry = self.addRecursively( h1.next, h2.next)
  
          print "h1.val: ", h1.data," h2.val: ", h2.data, " Carry: ", carry
          num = carry
          if h1:
              num +=h1.data
          if h2:
              num +=h2.data
  
      # now return the carry of the current addition 
          carry_ret = 1 if num &gt;= 10 else 0
  # this should be set somewhere in self or one of the lists?
          num = num % 10
          self.saveToLL(num)
          return carry_ret
  </code></pre>