Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the 'head' in a linked list set to none?

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? 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading