To delete all occurrences of an elements from a list, we can proceed as for the deletion of the first occurrence. The differences are that:
Observations:
Example: Suppose we have to delete the element "B" from the list shown in the figure:
Iterative implementation:
public static ListNode deleteAll(ListNode lis, String s) { ListNode p = new ListNode(); // create the generator node p.next = lis; lis = p; while (p.next != null) { if (p.next.info.equals(s)) p.next = p.next.next; // delete the element else p = p.next; } return lis.next; // delete generator node }