So i was trying to write a function that takes the head of a linked list and a data as arguments and appends a node at the tail of the linked list. And it seens to work fine (note that the head of the linked list isn't a node initially, it is equal to None actually) when it prints the 'new' head from inside the function (head becomes a node where head.data = 0 and head.next = None), but when i print the linked list's head after calling the function it doesn't return a node, in fact it returns None, meaning that the function didn't inserted the node at the tail which is initially the head. Can someone explain why the head's value isn't changing?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def InsertNode(head, data):
if head == None:
print("head == None, so head will be a node now")
head = Node(data)
print("now, head is a node where head.data = {} and head.next = {}".format(head.data, head.next))
else:
tail = head
while tail.next != None:
tail = tail.next
tail.next = Node(data)
def PrintLL(linkedList):
node = linkedList.head
while node != None:
print (node.data)
node = node.next
llist = LinkedList()
##print("llist.head == {}".format(llist.head))
InsertNode(llist.head, 0)
print("after InsertNode with data = 0, llist.head == {}".format(llist.head))