How do I remove an interior node from a singly linked list? I am using a node class with a double data member and a list class.
Printable View
How do I remove an interior node from a singly linked list? I am using a node class with a double data member and a list class.
I think this should work.
Code://If s->Next is the Node that you want to delete then do this
Node* temp;
temp = s->Next;
s->Next = s->Next->Next;
delete temp;
but what if the node you want to remove is s? how do i access the previous->next that points to s, and switch that to s->next?
What you do is iterate through the list until s->Next is the Node that you wanna delete, that's all.
Note: This will only work if you have a head node that you don't use.
//Edit: Here's an example
Code:while( s->Next != NULL )
{
if( s->Next->data == valueToDelete )
{
Node* temp;
temp = s->Next;
s->Next = s->Next->Next;
delete temp;
break;
}
s = s->Next; //Forgot this
}