Skip to content

Instantly share code, notes, and snippets.

@chelseadole
Created December 7, 2017 18:00
Show Gist options
  • Save chelseadole/c447920459ce8f3a2c8d6d85b014d60b to your computer and use it in GitHub Desktop.
Save chelseadole/c447920459ce8f3a2c8d6d85b014d60b to your computer and use it in GitHub Desktop.
"""Add Linked Lists of Numbers."""
def add_lists(list1, list2):
"""Function to create new LL based off sums of both other LLs."""
added_link = LinkedList()
temp = []
leftover = 0
while list1.head is not None and list2.head is not None:
if list1.head:
val_1 = list1.pop()
else:
val_1 = 0
if list2.head:
val_2 = list2.pop()
else:
val_2 = 0
sum = val_1 + val_2 + int(leftover)
if sum < 10:
temp.append(sum)
leftover = str(0)
else:
leftover = str(sum)[:-1]
temp.append(str(sum)[0])
for i in temp.reverse():
added_link.push(temp[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment