So for the following code I have been trying to use singly linked lists in python to calculate the sum of a list based on the even numbers within that list. I've written the code for the linked list portion I believe but I'm stumped on how to get it to actually take the even numbers only and sum them. Right now my code looks something like this:
def createList(plist):
linkedList = None
# goes backwards, adding each element to the beginning
# of the list.
for index in range(len(plist)-1, -1, -1):
linkedList = insertValueHead(linkedList, plist[index])
return linkedList
def sumEvens(linkedList): #This is what I'm looking for help with
.... #code
def testSumEvens():
myList = createList([14, 21, 29, 2, 16, 49, -26])
print "The sum of the even numbers in the first list is ", sumEvens(myList)
myList = createList([])
print "The sum of the even numbers in an empty list is ", sumEvens(myList)
myList = createList([5, 15, 25])
print "The sume of the even numbers in the final list is ", sumEvens(myList)
How would I go about making this create a sum of these lists? Such as in the first, 14 + 2 + 16?