Implement a helper function for the LinkedList class void L
Implement a helper function for the LinkedList class :
void LinkedList::removeAfter(Node * pre)
The job of this function is to remove the node right after pre. (You can assume that pre and pre->next are not null.) The function should also decrement length since the linked list will be one node shorter than before the function was called.
Remember: to remove the node after pre you need to save the node\'s address in a temp variable , then set pre->next to temp->next. Finally you should delete temp and decrement length.
The function will be a helper function for removeValue and removeAtPosition. That means you are allowed to call it when you implement those functions.
Solution
As you described in question that pre and pre->next are not null so I am discarding two conditions in function.
void LinkedList::removeAfter(Node *pre) {
if (pre->next->next == NULL) {
Node *temp = pre->next;
pre->next = NULL;
delete temp;
}
else {
Node *temp = pre->next;
pre->next = temp->next;
delete temp;
}
}
