You are the surest way to return the current function to remove the last node. The problem is that you identify the penultimate node with curr.next == null , if you received it, you invalidate it, but this is your actual input! So, you should check if the node antepenultimate node in the list is really:
if (curr.next.next == null) { curr.next = null;
With this change, there are more basic cases to check, but it is up to you, my friend.
source share