Pergunta de entrevista da empresa Microsoft

Given only a pointer to an arbitrary node in a singly-linked list, how would you delete that node? Are there any cases where this is impossible?

Respostas da entrevista

Sigiloso

7 de jul. de 2009

OK, it bothered me that this was too simple so I looked around. You can do it, sort of. You copy the data from the next node to the target node, link the target node to the node after the next and then free the next node. Not the behavior you would expect, and dangerous if any pointers to the freed node are still around. IMHO it's this kind of trick programming that results in unstable and hard-to-maintain code, so if this is the correct answer, then you know what to expect from this organization. I'd love to learn of a better way.

2

Sigiloso

25 de jul. de 2009

In response to Ken's answer: Why do you think MS WindoZe is SO unstable? (rhetorical) LOL

Sigiloso

29 de jul. de 2009

You can do it (without copying) if the list is designed to be circular, cycling through till you get to this node again, and remembering what the previous node was.

Sigiloso

3 de jul. de 2009

It's a trick question, right? You can't do as described because you need a pointer to the previous node if you are to connect it to the node after the deleted node. If I'm right, can I get a free Win7 upgrade?