Wednesday, February 7, 2007

Remove Node in Linked List

This is apparently a question from Microsoft.

Given a singly linked list. You're given a pointer to a node in the list. Give me an algorithm that efficiently removes that node from the list.

Solution:

Some might consider this solution a cheat, but really it's quite elegant. Obviously, you can traverse the entire list up to that node, but equally obvious is that it's not the solution they'll be looking for. You should do this: copy the data from the next node into this node. Now erase the next node and update where the given node is pointing to.

Unless copying data is the bottleneck, this should be the best solution in most cases. Perhaps there are times when the address of the node is critical, but not in an interview question, and certainly not in this interview question.

1 comment:

Anonymous said...

Toenail Medicines Over The Counter fartonast

cheap soma online
Tell your doctor if you are on other drugs medication.
[url=http://www.esavannahevents.com/]soma without prescription[/url]
You can order Soma at reliable online pharmacy and save money.
http://www.esavannahevents.com/ - buy cheap soma

Soma is enlisted in a group of drugs called muscle relaxants.


buy diazepam
Valium can be used for the treatment of anxiety, sleeping disorder, muscle spasms and few kinds of epilepsy.
[url=http://www.boomercaraudio.com/]buy valium[/url]
On the contrary, foods with acidic property can make the absorption process faster, which leads to decreasing level of drug’s function.
http://www.boomercaraudio.com/ - valium pharmacy
Alleviate anxiety Buy low cost Valium (Diazepam) drugs at one of reliable online pharmacies and save big!
A brand medicine of medicine group benzodiazepines, Valium is produced and marketed by Roche to treat patients suffering from anxiety.