Algorithm for deleting one element in an single linked list with O(1) complexity -
I am a student of computer science in Germany. My professor thought of the following question:
'A node is referred to in a linked list (which is not the last node)
I thought about it, but I'm pretty sure there is no such algorithm, give an algorithm to remove this element from the list which maintains the o complexity (1). Because this is a single link list, you need a loop through each node in the list, until you reach the node, which should be removed, because you need to modify the next-pointer in the node before it's removed . It depends on whether nodes are volatile (in the value) or not.
One way to do this is , if you can do whatever you want with the nodes:
toDelete.value = toDelete.next All information from toDelete now old toDelete.next from .value toDelete.next = toDelete.next.next Is overwritten. (Depending on the platform, you may need to free the old toDelete.next - it means to have a temporary reference. It is not good that someone else still has a reference Java / C # you want to ignore it.)
I tried to prepare a way to gesture it without giving it, but it is a bit difficult ...
< P> It does rely on the final node in this list is not.
Comments
Post a Comment