I was reading through the code in the Crashing the Coding Interview book and when they delete a node in page 93, this is a snippet of the code they use:
Node deleteNode(Node head, int d){
while (n.next != null) {
if (n.next.data == d){
n.next == n.next.next;
return head;
}
return head;
}
}
What I don't understand is why this deletes the previous n.next node if the previous n.next node would still have a link to the node after it? I attached an illustration the help explain.
Thanks in advance!
b
- unable to reach from any live thread of execution) will be garbage collected.b
to their static reference).