python - How to print the elements of a linked list? -


i'm dong node exercise on python today. seem have accomplished part of it, not complete success.

class node:     def __init__(self, cargo=none, next=none):         self.cargo = cargo         self.next  = next      def __str__(self):         return str(self.cargo)  node1 = node(1) node2 = node(2) node3 = node(3)  node1.next = node2 node2.next = node3  def printlist(node):   while node:     print node,     node = node.next   print 

so original __init__, __str__ , printlist, makes like: 1 2 3.

i have transform 1 2 3 [1,2,3].

i used append on list created:

nodelist = []  node1.next = node2 node2.next = node3   def printlist(node):     while node:         nodelist.append(str(node)),          node = node.next 

but in list within string, , don't want that.

if eliminate str conversion, memory space when call list print. how unstringed list?

instead of calling str() on node, should access it's cargo:

. . .     while node:     nodelist.append(node.cargo)     node = node.next . . .