educative.io

Question on Singly Linked List Deletion (Implementation)

The implementation is simply in 2 lines
“headNode = headNode.nextNode;
size–”
To my understanding, the headNode variable is placed with another variable which is the next node. So did we just overwrite the headNode or we call the next node headNode?
Untitled Diagram11

2 —> 3 **is the right assumption. As java garbage collection principle states that if an object doesn’t have any reference then that object will be destroyed once garbage collector gets called. **
Now, a confusion may raise from the implementation, if we move head pointer to 2 but still 1st node store the address of 2nd node, then how it will be deleted. In that case, but not necessarily we can use one more pointer where we will store the headNode. Then we move the headNode from 1 to 2 and set nextNode in currentNode pointer as null, that’s way we can fully detach 1st node from the chain.

2->3 should be correct answer.

1 Like

agree