# Sum of the alternate nodes of linked list in python

Python program for Sum of the alternate nodes of linked list. Here problem description and explanation.

``````#  Python 3 program for
#  Sum of the alternate nodes of linked list

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :
def __init__(self) :

def insert(self, value) :
#  Create a node
else :
#  Find last node
while (temp.next != None) :
#  Visit to next node
temp = temp.next

#  Add node at last position
temp.next = node

#  Display all Linked List elements
def display(self) :
while (temp != None) :
#  Display node value
print("  ", temp.data, end = "")
#  Visit to next node
temp = temp.next

else :

#  Sum of all alternate nodes of linked list
def sumAlternateNode(self) :
#  Get first node
sum = 0
#  Iterate the elements of linked list,
#  And sum of all alternate element.
while (temp != None) :
#  Sum node value
sum += temp.data
if (temp.next != None) :
#  Visit to next second node
temp = temp.next.next
else :
#  When no alternate node
#  Like break
temp = None

#  Display result
print("\n Sum of all alternate nodes : ", sum)

def main() :
sll = SingleLL()
#  6 → 2 → 1 → 4 → 3 → 3 → 4 → 8 → NULL
sll.insert(6)
sll.insert(2)
sll.insert(1)
sll.insert(4)
sll.insert(3)
sll.insert(3)
sll.insert(4)
sll.insert(8)
sll.display()
sll.sumAlternateNode()

if __name__ == "__main__": main()``````

Output

`````` Linked List
6   2   1   4   3   3   4   8
Sum of all alternate nodes :  14``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.