This is some hack I discovered while solving the problem (delete every even node in the list)
internal void DeleteNode(int p) { DeleteNode(head, p); } private void DeleteNode(Node head, int p) { Node current = head; Node prev = null; while (current.next != null) { prev = current; current = current.next; if (current.data == p) { prev.next = current.next; } } }
Now here, in anticipation, you assign a current, and then move the current to the next, so prev contains the previous node. Hope this helps ...
source share