views:

1314

answers:

6

I'm a student of computer science in Germany. My professor gave use the following question to think about:

'Given a reference to a node in a single linked list (which is not the last node). Give an algorithm to delete this element from the list which has O(1) complexity while maintaining the integrity'.

I thought about this, but I'm pretty sure, that there is no such algorithm. since it is a single linked list, you must loop through every node in the list until you reach the node which should be delete, because you have to modify the next-Pointer in the node before the deleted. Which would lead to O(n) complexity.

Am I missing anything?

+13  A: 

It depends on whether or not the nodes are mutable (in value).

There is a way of doing it, if you can do what you like with the nodes:

toDelete.value = toDelete.next.value
toDelete.next = toDelete.next.next

All the information from toDelete has now been overwritten, by the information in the old toDelete.next. (Depending on the platform, you may then need to free the old toDelete.next - which means keeping a temporary reference to it. Not good if anyone else still has a reference to it, of course. In Java/C# you'd just ignore it.)

I tried to work out a way of hinting at it without giving it away, but it's kinda tricky...

It does rely on this not being the last node in the list though.

Jon Skeet
Ah, got it. Thanks :)
Martin
be careful, as Jon explained, that for this to work the Node structure needs to be private. None should hold a reference to the nodes themselves, only to the data. Otherwise consumers would be left with an invalid reference to the old toDelete.next object (if you are in C++ for example and you deleted the node structure), or at best an invalid node object.
Denis Troller
This isn't really an 'algorithm' for removing an element from a singly-linked list is it? This is the standard method of removing an element from the list.
Dinuk
@Dinuk: Normal deletion in a linked list where you've got appropriate information just involves updating the next (and potentially prev) links - not copying the data from the "next" node onto this one. At least, that's what I'd normally do :)
Jon Skeet
The issue here is that you don't have a link to the previous node and therefore must move the node following the deleted to the memory location that the previous node is pointing to. Otherwise, the list is broken. So it is a little different than normal delete where previous node is know.
Joe
+7  A: 

Not exactly considered deleting a node but you can copy the data of the next node to the current and delete the next node:

// pseudocode:
this.data = next.data;
var temp = this.next;
this.next = next.next;
delete temp;
Mehrdad Afshari
A: 

Yes. You keep as your iterating pointer, a pointer to the next pointer of the previous list.

walk = &head;
while (!match(*walk)) {
    walk = &walk[0]->next;
    if (!*walk) return ;
}
*walk = walk[0]->next;
Joshua
That's not O(1) though.
Jon Skeet
The remove part is.An updating iterator on a linked list should *always* use pointer to pointer to next node.
Joshua
+1  A: 

If the nodes are basic structures in memory, you can copy the contents of the "next" node into the memory location of the deleted node, and deallocate the memory where the "next" node used to be.

Dmitry Brant
+1  A: 

Assuming you have the pointer to the node you wish to delete. Replicate the next value into the current node. Replace the current pointer to the node the next pointed at.

Joe
+1  A: 

move the data, not the link. look to this other thread: http://stackoverflow.com/questions/69209/deleting-a-middle-node-from-a-single-linked-list-when-pointer-to-the-previous-nod

pomarc