Friday, May 18, 2007

Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?

This is a very good interview question
The solution to this is to copy the data from the next node into this node and delete the next node!. Ofcourse this wont work if the node to be deleted is the last node. Mark it as dummy in that case. If you have a Circular linked list, then this might be all the more interesting. Try writing your own C program to solve this problem. Having a doubly linked list is always better.

150 comments: