我的问题是,一旦我们到达 remove()函数的末尾,对象 Node(3)是否仍会在RAM内存中?如果不是,什么时候从RAM存储器中删除它?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add(self, data):
if self.head is None:
self.head = Node(data)
else:
curr_node = self.head
while (curr_node.next):
curr_node = curr_node.next
curr_node.next = Node(data)
def remove(self, data):
if self.head is not None:
if self.head.data == data:
self.head = self.head.next
else:
curr_node = self.head
while curr_node.next is not None:
if curr_node.next.data == data:
curr_node.next = curr_node.next.next
break
curr_node = curr_node.next
l = LinkedList()
l.add(2)
l.add(3)
l.add(4)
l.remove(3)
# program going on ...