def sumLists(list1Node, list2Node, carry=0): if not (list1Node or list2Node or carry > 0): return None sumNode = Node(carry) list1Next = None list2Next = None # Add the listNode only if necessary, this handles varying lengths if list1Node: sumNode.value += list1Node.value list1Next = list1Node.next if list2Node: sumNode.value += list2Node.value list2Next = list2Node.next carry = sumNode.value / 10 sumNode.value = sumNode.value % 10 # hands off carry and next nodes of both lists recursively nextSumNode = sumLists(list1Next, list2Next, carry) # After node is received, take current node and put in front sumNode.next = nextSumNode return sumNode