Reputation: 1490
I am picking up linked lists in python. Basically I'm writing a logic for adding a node at the front of my linked list. But, my teacher did it a little different and so I need to understand why there is a need for the following piece of line.
Here is the entire code for reference.
#Singly linked list in Python3
class SLLNode:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return "SLLNode object data = {}".format(self.data)
def get_data(self):
return self.data
def set_data(self, new_data):
self.data= new_data
def get_next(self):
return self.next
def set_next(self, new_next):
self.next= new_next
#Singly Linked List class
class SLL:
def __init__(self):
self.head= None
def __repr__(self):
return "SLL object head: {}".format(self.head)
def isempty(self):
return self.head is None
def add_front(self, new_data):
temp = SLLNode(new_data)
temp.set_next(self.head) # Why do we need this line?
self.head= temp
So in the add_front
method of SLL
class, why do we need to go through setting set_next=None
by doing this
temp = SLLNode(new_data)
temp.set_next(self.head)
self.head= temp
When I see the next pointer is being set to None
by default in the SLLNode
class.And so I do this and get the same output.
temp = SLLNode(new_data)
self.head= temp
Is it some case I am missing on in here that my teacher did not explain? Or my program will do fine without that extra line?
Upvotes: 0
Views: 413
Reputation: 1490
Although I understood and accept orlp's answer, I would like to clarify it by breaking it down into a much simpler sentence.
Basically, our self.head
can point to None
or a linked list with nodes.
Now, if latter is the case we cannot insert our new node simply by doing
temp = SLLNode(new_data)
self.head= temp
This is basically creating a node with a value in it and makes the self.head
points to it. But while doing this, we break the link to the other chain of already present linked list to which our self.head
was pointing to before.
So to systematically summarize,
Step 1 : We initialize our new/temp
node.
temp=SLLNode(new_data)
Here we just make a node and initialize it with a value. the self.next
pointer will be None at this time.
Step 2 : We make out newly initialized node point to the same place where the self.head
is currently pointing to.(It could be pointing to None
or a node. So if None
is the case then my logic of commenting out temp.set_next(self.head)
works but if its pointing to a node then my logic breaks the connection to the existing chain and self.Head
now points to the temp
/ newly created node only, which is wrong because by doing this we have broken the connection to existing chain. That's the reason why we do the below piece of code.It ensures that my newly created node/temp
also points to the same place where self.head
does and so now even if we break the connection of self.head
we have our chain of linked list secure.)
temp.set_next(self.head)
Step 3 : Now we just make the self.head
point to the temp
/newly created pointer, so that we have a complete chain.
self.head= temp
Just a side note - I found it a bit confusing in the beginning and I don't want others to weather that discomfort when they have this confusion which is why my steps are purposefully collaborative. Hope it helps :)
Upvotes: 0
Reputation: 117771
Your observation is true if you only ever call add_front
once.
But the second time you call add_front
, self.head
is no longer None
, and actually points to the previous head.
To visualize, this is what happens the first time you call add_front
:
Before: None
After: A -> None
You create a new node (which we'll call A) and make it point to the previous head, which is None. But the second time this happens:
Before: A -> None
After: B -> A -> None
We create another node (B), and make it point to the previous head. But now the previous head is no longer None, it is A!
Upvotes: 2