Error de atributo: El objeto ‘LinkedList’ no tiene el atributo ‘head’.
No entiendo qué está mal con mi código. Dice:
AttributeError: el objeto ‘LinkedList’ no tiene el atributo ‘head’
Aquí está mi código:
class Node:
def init(self, data):
self.data = data
self.next = None
Creé una lista enlazada vacía y agregué nodos:
class LinkedList():
def int(self):
self.head = None
def insert(self, newNode):
if self.head is None:
self.head = newNode
else:
#head -> john->Ben->Matthew->None
lastNode = self.head
while True:
if lastNode.next is None:
break
lastNode = lastNode.next
lastNode.next = newNode
def printList(self):
#head -> john->Ben->Matthew->None
currentNode = self.head
while True:
if currentNode is None:
break
print(currentNode.data)
currentNode = currentNode.next
node -> data, next
#firstnode.data -> data, firstnode.next -> None
primerNodo = Nodo(‘john’)
listaenlazada = LinkedList()
listaenlazada.insert(primerNodo)
segundoNodo = Nodo(‘Ben’)
listaenlazada.insert(segundoNodo)
tercerNodo = Nodo(‘Matthew’)
listaenlazada.insert(tercerNodo)
listaenlazada.printList()
davy.ai
El problema está en el código de la clase “LinkedList” en el método “int“. Está mal escrito y debería ser “init“. Cambie esto y vuelva a ejecutar el código, debería funcionar como se espera.