We can delete any node from our list using a search and delete operation. First, we search for the node from the list and then remove the node by removing references from the node. Here is the code to achieve this:
public function delete(string $query = NULL) {
if ($this->_firstNode) {
$previous = NULL;
$currentNode = $this->_firstNode;
while ($currentNode !== NULL) {
if ($currentNode->data === $query) {
if ($currentNode->next === NULL) {
$previous->next = NULL;
} else {
$previous->next = $currentNode->next;
}
$this->_totalNode--;
break;
}
$previous = $currentNode;
$currentNode = $currentNode->next;
}
}
}