I am kinda new to DataStructure and I trying to write LinkedList, but I cannot figure out why my insert method does not work. If you have any suggestions to improve, please write to me
class Node:
def __init__(self, data):
self.item = data
self.ref = None
class LinkedList:
def __init__(self):
self.start_node = None
self.index = 0
def append(self, val):
new = Node(val)
if not self.start_node:
self.start_node = new
return
last = self.start_node
while last.ref:
last = last.ref
last.ref = new
self.index += 1
def insert(self, index, value):
start_counting = 0
start = self.start_node
while start_counting < index:
start = start.ref
start_counting += 1
NewNode = Node(value)
tmp = start.ref
start = NewNode
start.ref = tmp
def display(self):
ls = []
last = self.start_node
while last:
ls.append(last.item)
last = last.ref
return ls
start.next = tmpWhat is the next attribute? Your definition of the Node class has a ref attribute, not a next attribute.