Reputation: 16489
I was doing some problems in python specifically this one:
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node
The solution is this:
def deleteNode(linkedlist, node):
if node.next != None:
node.value = node.next.value
node.next = node.next.next
else:
node.value = None
I've done some Java before and the A = B
with objects means A refers to B. According to this problem. Does this mean in the python expression A = B
that A deep-copies B?
Upvotes: 0
Views: 52
Reputation: 282042
No. A = B
works pretty much the same in Python as it does in Java. (There are a few differences in certain advanced use cases, but those aren't important here.)
This function doesn't actually delete the node. It cheats; the next node's data gets copied into this node, and the next node is deleted. As long as no one else was relying on the identity of specific nodes in the list, and as long as there is a next node, it works okay. (The else
that looks like it handles the case where there is no next node is actually bugged; instead of deleting the data from the list, it replaces the data with a spurious None
value.)
Upvotes: 4