Created
June 28, 2014 18:20
-
-
Save yokiy/62724b32df4c2dc58121 to your computer and use it in GitHub Desktop.
cc 2.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cc 2.5 addition with reverse digit | |
from LinkedList import Node, LinkedList | |
def addReverse(ls1, ls2): | |
add1 = ls1.head | |
add2 = ls2.head | |
carrier = 0; | |
addresult = LinkedList() | |
if add1 is None and add2 is None and carrier == 0: | |
return | |
while add1 is not None and add2 is not None: | |
result = add1.data + add2.data + carrier | |
carrier = 0 | |
if result >= 10: | |
carrier = 1 | |
result = result - 10 | |
addresult.AddNode(result) | |
add1 = add1.next | |
add2 = add2.next | |
while add1 is None and add2 is not None: | |
result = add2.data + carrier | |
carrier = 0 | |
if result >= 10: | |
result = result - 10 | |
carrier = 1 | |
addresult.AddNode(result) | |
add2 = add2.next | |
while add1 is not None and add2 is None: | |
result = add1.data + carrier | |
carrier = 0 | |
if result >= 10: | |
result = result - 10 | |
carrier = 1 | |
addresult.AddNode(result) | |
add1 = add1.next | |
while add1 is None and add2 is None and carrier == 1: | |
addresult.AddNode(carrier) | |
carrier = 0 | |
print 'the result of addition is' | |
addresult.printList() | |
#test case | |
ls1 = LinkedList() | |
ls1.AddNode(7) | |
ls1.AddNode(1) | |
ls1.AddNode(8) | |
ls1.AddNode(9) | |
ls2 = LinkedList() | |
ls2.AddNode(5) | |
ls2.AddNode(9) | |
ls2.AddNode(2) | |
ls1.printList() | |
print '+' | |
ls2.printList() | |
addReverse(ls1, ls2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment