These are my code
class Node():
'''A node in a linked list'''
def __init__(self, data, next_node=None):
self.data = data
self.next_node = next_node
def sum(LL):
head = LL
# If Linked list is empty, return 0
if (head.data is None or head.data == []):
result = 0
# If Linked List contain only 1 node then return it
elif (head.next_node is None):
result = head.data
else:
curr = head
result = curr.data
# If next is not None then add up to result
while (curr.next_node is not None):
curr = curr.next_node
result += curr.data
return result
Problem is at the end of the code
while (curr.next_node is not None):
curr = curr.next_node
result += curr.data
If i try this
LL = Node(1, Node(2, 3))
sum(LL)
I don't understand why it said
builtins.AttributeError: 'int' object has no attribute 'data'
The while loop works until it reach the last node
result should be 6