How come newNode object in insert method is having different heap addresses while the newNode reference variable is same ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How come newNode object in insert method is having different heap addresses while the newNode reference variable is same ?

# A single node of a singly linked list class Node: # constructor def __init__(self, data = None, next=None): self.data = data self.next = next # A Linked List class with a single head node class LinkedList: def __init__(self): self.head = None # insertion method for the linked list def insert(self, data): newNode = Node(data) if(self.head): current = self.head while(current.next): current = current.next current.next = newNode else: self.head = newNode # print method for the linked list def printLL(self): current = self.head while(current): print(current.data) current = current.next # Singly Linked List with insertion and print methods LL = LinkedList() LL.insert(3) LL.insert(4) LL.insert(5) LL.printLL()

4th Jul 2020, 8:27 PM
Parth Lashkari
Parth Lashkari - avatar
2 Answers
0
I m talking about object newNode in insert method , i m asking how come that newNode is getting different heap addresses on each insert method call since the object name (newNode) is same each time.
4th Jul 2020, 9:05 PM
Parth Lashkari
Parth Lashkari - avatar
0
How come 1st and 3rd object heap address r coming same? !! Python programming doubt please help Class Test: def __init__(self,data): self.data = data print(self.data) obj = Test(5) print(obj) obj = Test(6) print(obj) obj = Test(7) print(obj) Then why these obj objects are not getting different heap addresses coz output heap address of 1st and 3rd object is coming same
4th Jul 2020, 9:13 PM
Parth Lashkari
Parth Lashkari - avatar