вы можете попробовать что-то вроде этого:
a_value = [1,2,3]
b_value = [4,5,6]
c_value = [7,8,9]
d_value = [10,11,12]
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value)
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
node = arraylist.headval # fetch head node
nextval = node.nextval # fetch next to node
list = []
list.append(node.dataval) # enter all head list
while nextval: # this loop will break as nextval is none
node = node.nextval # node now becomes next and repeat
if hasattr(node, 'nextval'):
nextval = node.nextval
else:
nextval = None
list.append(node.dataval)
sum = 0
for x in list: # sum in the end or break into 23 for all separate
for y in x:
sum+=y
print(list, sum)
#OUTPUT [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 78
Работает нормально:)