Deleting a node from a linked list?

You are give a node in a linked list and you need to delete it from the list..

You are only given the pointer to that node and nothing else? is this possible?

Title

Hi

explaination

the problem is that we have to delete the node.. but dont know the node before that ... so what we can do is copy the content of the node next to it in the current node .. and simply delete the next node...instead of transfering all teh content iteratively till the end

Deleting a node from a linked list?

Sounds a bit out of context with the actuall questions... or maybe I didn't get you.. could you explain

slightly better way

we can simply transfer content of the element next to the element to be deleted and delete the next element(because for it we know the elemetn before it and after it ) so we dont need to traverse the list..

Deleting a node from a linked list?

Thou art right.
I stand corrected.

Deleting a node from a linked list?

You just made the solution I gave more complicated.. but the quesiton is still flawed...

What if the given node to be deleted is the last node in the list??. Now you need to make the previous nodes next pointer to NULL.. and you cannot do this..

Deleting a node from a linked list?

the question is not flawed. there is a solution.
When i have the information about one node, I have information about all the subsequent nodes though the node->next pointer. The information I do not have is the nodes that are before the current node in the list.

And I really dont need that information to delete the current node.

take a list that looks like this:

x->x->x->x->A->B->C->D->NULL

you are given a pointer to A, and nothing else. You have no way of finding out anything about the x s

To delete A, copy B into A, C into B, D into C and delete node D. Voila.

Code

[code:1]DeleteNode (Node* current)
{
while (current->next)
{
current-> value = current->next->value ;
current=current->next;
}
delete current;
}[/code:1]

Deleting a node from a linked list?

Well,, look at the code again.. i am copying the next nodes data in the current node and deleting the next node..

Also you solution does not apply since there is only one node that you have information about..

But the bottom line.. this is a flawed question and there is no real solution for this...

Deleting a node from a linked list?

geekgod, your solution will not work. you are duplicating next node and then deleting the current node. as soon as you do that, the connection from the previous node to the next node is lost.

There is a way around this, though very inefficient. If you know the address to some intermediate node that you want to delete,

copy the next node's contents into current node,
move to next node and repeat, till last node is reached. after copying the contents of last node into last-but-one node, delete the last node.

basically you are moving all the elements one position to the left and deleting the last node.

this should work for any position..

R

Deleting a node from a linked list?

I have seen this question asked in interviews but this is a flawed question.

The known solution for this is to do this:

currentNode->Data = nextNode->Data
currentNode->Next = nextNode->Next
delete nextNode

Basically you are copying the next Nodes data into the current Node and then delete the next Node... it basically has the same effect of deleting the current Node.. but the reason this is flawed is because this is not possible when the node to be deleted is the last node in the list

Deleting a node from a linked list?

Singly linked list:
If you want to keep the rest of the linked list intact after deletion of the given node (so that someone who does have the head of the list can traverse the whole list), then it is not possible.

Doubly linked list: It is always possible: simply put

p->parent->child=p->child;
delete (p);

struct node

struct node *temp1,*temp2;
temp1=NULL;
temp2=head;

if(head->ele==ele_to_be_deleted)
{
temp1=head;
head=head->next;
free(temp1);
exit(0);
}

while(temp2!=NULL &&temp2->ele!=ele_to_be_deleted)
{
temp1=temp2;
temp2=temp2->next;
}

if(temp2->ele==ele_to_be_deleted)
{
temp1->next=temp2->next;
free(temp2);
}

if(temp1->ele==ele)
{
temp1->next=NULL:
}