Deleting a middle node from a single linked list when pointer to the previous node is not available

60,687

Solution 1

Let's assume a list with the structure

A -> B -> C -> D

If you only had a pointer to B and wanted to delete it, you could do something like

tempList = B->next;
*B = *tempList;
free(tempList);

The list would then look like

A -> B -> D

but B would hold the old contents of C, effectively deleting what was in B. This won't work if some other piece of code is holding a pointer to C. It also won't work if you were trying to delete node D. If you want to do this kind of operation, you'll need to build the list with a dummy tail node that's not really used so you guarantee that no useful node will have a NULL next pointer. This also works better for lists where the amount of data stored in a node is small. A structure like

struct List { struct List *next; MyData *data; };

would be OK, but one where it's

struct HeavyList { struct HeavyList *next; char data[8192]; };

would be a bit burdensome.

Solution 2

Not possible.

There are hacks to mimic the deletion.

But none of then will actually delete the node the pointer is pointing to.

The popular solution of deleting the following node and copying its contents to the actual node to be deleted has side-effects if you have external pointers pointing to nodes in the list, in which case an external pointer pointing to the following node will become dangling.

Solution 3

I appreciate the ingenuity of this solution (deleting the next node), but it does not answer the problem's question. If this is the actual solution, the correct question should be "delete from the linked list the VALUE contained in a node on which the pointer is given". But of course, the correct question gives you a hint on solution. The problem as it is formulated, is intended to confuse the person (which in fact has happened to me, especially because the interviewer did not even mention that the node is in the middle).

Solution 4

One approach would be to insert a null for the data. Whenever you traverse the list, you keep track of the previous node. If you find null data, you fix up the list, and go to the next node.

Solution 5

The best approach is still to copy the data of the next node into the node to be deleted, set the next pointer of the node to the next node's next pointer, and delete the next node.

The issues of external pointers pointing to the node to be deleted, while true, would also hold for the next node. Consider the following linked lists:

A->B->C->D->E->F and G->H->I->D->E->F

In case you have to delete node C (in the first linked list), by the approach mentioned, you will delete node D after copying the contents to node C. This will result in the following lists:

A->B->D->E->F and G->H->I->dangling pointer.

In case you delete the NODE C completely, the resulting lists will be:

A->B->D->E->F and G->H->I->D->E->F.

However, if you are to delete the node D, and you use the earlier approach, the issue of external pointers is still there.

Share:
60,687
Nitin
Author by

Nitin

Updated on September 21, 2020

Comments

  • Nitin
    Nitin over 3 years

    Is it possible to delete a middle node in the single linked list when the only information available we have is the pointer to the node to be deleted and not the pointer to the previous node?After deletion the previous node should point to the node next to deleted node.

  • Nitin
    Nitin over 15 years
    Some one asked me this question in interview.
  • Dave L.
    Dave L. over 15 years
    Marching down the list still gives O(n) to delete an item.
  • Raik
    Raik over 15 years
    Indeed. I was referring to deleting the whole list (randomly).
  • Arafangion
    Arafangion almost 14 years
    One issue, though, is that maintaining that flag will increase memory for every item.
  • codingfreak
    codingfreak about 13 years
    I would say Copy the NEXT part of NODE B into temp. Then copy all the contents of NODE C into NODE B. Delete NODE C using the address in temp varaible.
  • Tony Delroy
    Tony Delroy about 13 years
    +1: not only did you apparently beat Mikhail to this answer, but you've explained some of the dangers... weird you've got 10% of his answer's votes...
  • Neowizard
    Neowizard over 11 years
    If you ask me, a node in a linked list is identified by its data and it's position in the list and not by it's location in memory, so I think the 2 top-voted answers are a perfect solution
  • Trying
    Trying almost 11 years
    totally different solution. He asked to delete a number in the middle no necessarily the middle element. :-)
  • Rakesh Patil
    Rakesh Patil over 9 years
    basically its assigning one node back till end.