def sum_lists(ll_a,ll_b): a_str = "" b_str = "" a_curr = ll_a.head b_curr = ll_b.head while a_curr: a_str = str(a_curr.data) + a_str a_curr = a_curr.next while b_curr: b_str = str(b_curr.data) + b_str b_curr = b_curr.next total = int(a_str) + int(b_str) out = LinkedList() while total: remainder = total % 10 temp_node = Node(remainder) if not out.head: out.head = temp_node else: out.add_node(temp_node) total = (total - remainder)/10 return out
def alt_sum_lists(ll_a, ll_b): a_curr = ll_a.head b_curr = ll_b.head sum_ll = LinkedList() carryover = 0 while a_curr or b_curr: total = carryover if a_curr: total += a_curr.data a_curr = a_curr.next if b_curr: total += b_curr.data b_curr = b_curr.next sum_ll.add_node(Node(total%10)) carryover = total/10 if carryover: sum_ll.add_node(Node(carryover)) return sum_ll