Why is it only printing 6 and 7? I need to print all the values. I believe there is some issue with get() as self.head is being updated somewhere? If so, how can I declare self.head here as the beginning so that it prints all the elements. Thanks in advance.
class Node:
def __init__(self,data):
self.data = data
self.next = None
class list:
def __init__(self):
self.head = None
def prepend(self,data):
n = Node(data)
if self.head is None:
self.head = n
else:
n.next = self.head
self.head = n
def append(self,data):
n = Node(data)
if self.head is None:
self.head = n
else:
while self.head.next:
self.head = self.head.next
self.head.next = n
def get(self):
while self.head:
print(self.head.data)
self.head = self.head.next
la = list()
la.prepend(1)
la.append(4)
la.append(5)
la.append(6)
la.append(7)
la.get()
source https://stackoverflow.com/questions/73674604/python-linked-list
Comments
Post a Comment