This code is supposed to delete every node from a linked list with a given value.
Are there any logical errors with this implementation? Did I do anything wrong or is there an area of this implementation that needs significant improvement. Any suggestions?
public boolean delete(int d) {
Node tmpNode = head;
Node prevNode = null;
boolean deletedANode = false;
if (head == null) {
return deletedANode;
}
while (tmpNode != null) {
if (tmpNode.data == d) {
if (tmpNode == head) {
head = head.next;
}
else {
prevNode.next = tmpNode.next;
}
deletedANode = true;
}
prevNode = tmpNode;
tmpNode = tmpNode.next;
}
return deletedANode;
}
deleteAll(value)
. \$\endgroup\$