hi I been trying to teach myself about data structures, and started off with reading stuff about linked lists. Im still new to python in general, but I knew the basics of classes and the syntax and all that, so decided why not just try learning this. Anyways, I got up to this part where I create a linked list class, and I know that the ‘head’ part of a linked list will be the first node, but not too sure as to why it is set to ‘None’. Apologies if this is a dumb question :/
Here is the code to it…
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None #why is this part set to none?
>Solution :
it’s just intialization part, when linkedlist is intialise you dont expect it to have vaules.
ll = LinkedList()
node = Node(1)
ll.head = node
this will make node as the head of the linked list and you can add the values to it later