Reputation: 33
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))
Upvotes: 3
Views: 269
Reputation: 106995
You need to pass the LinkedList
object itself to InsertNode
instead of its head
attribute. By passing the head
attribute to InsertNode
, any new assignments made to it will not be reflected on the original object.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def InsertNode(linkedList, data):
if linkedList.head == None:
print("head == None, so head will be a node now")
linkedList.head = Node(data)
print("now, head is a node where head.data = {} and head.next = {}".format(linkedList.head.data, linkedList.head.next))
else:
tail = linkedList.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()
InsertNode(llist, 0)
print("after InsertNode with data = 0, llist.head == {}".format(llist.head.data))
Upvotes: 1
Reputation: 2414
The following is the fixed code:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def InsertNode(node, data):
if node.head == None:
print("head == None, so head will be a node now")
node.head = Node(data)
print("now, head is a node where head.data = {} and head.next = {}".format(node.head.data, node.head.next))
else:
tail = node.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, 0)
print("after InsertNode with data = 0, llist.head == {}".format(llist.head))
What was happening was you were passing in a pointer to the head:
InsertNode(llist.head, 0)
InsertNode would then assign a new node instance to that variable:
head = Node(data)
Since the head pointer is passed by assignment, the version of head
local to the function InsertNode
was changed, but the original llist.head
was not affected.
Upvotes: 1