I was following a tutorial about data structures and algorithms and the instructor in the course was using java script. The instructor implemented a Linked List in java script using a object/hash table/hash map. So i tried to do the same implementation in python and this is the code that i wrote.
class LinkedList():
def __init__(self,value):
self.head = {"value":value , "next": None}
self.tail = self.head
self.length = 1
def append(self,value):
new_node = {"value":value , "next":None}
self.tail["next"] = new_node
self.tail = new_node
self.length += 1
def prepend(self,value):
new_node = {"value":value , "next":self.head}
self.head = new_node
self.length += 1
def insert(self,index,value):
if index > self.length:
return "The index is greater than the Linked List"
elif index == 0:
self.prepend(value)
elif index == self.length:
self.append(value)
else:
prev_node = self.head
new_node = {"value":value , "next":None}
for i in range(0,self.length):
if i == index-1:
new_node["next"] = prev_node["next"]
prev_node["next"] = new_node
self.length += 1
break
else:
prev_node = prev_node["next"]
def delete(self,index):
current_node = self.head
if index >= self.length:
return "There is no such index in the list"
elif index == 0:
self.head = self.head["next"]
self.length += -1
else:
for i in range(0,self.length-1):
if i == index-1:
unwanted_node = current_node["next"]
current_node["next"] = unwanted_node["next"]
if index == self.length-1:
self.tail = current_node
self.length += -1
break
else:
current_node = current_node["next"]
def reverse(self):
current_node = self.head
new_head = LinkedList(current_node["value"])
while current_node["next"] != None:
current_node = current_node["next"]
new_head.prepend(current_node["value"])
self.head = new_head.head
self.tail = new_head.tail
But when i tried searching on the internet there was different implementation of linked list which doesn't consist of dictionaries. I just wanted to know that is the code that i have written is a successful implementation of Linked List in python or not