Simple Java Solution and Corner Cases

  • 3
    public class Solution {
        public void deleteNode(ListNode node) {
            if (node == null || == null) {
            node.val =;

  • 0

    It is redundant to have == null, which implies that node is tail. You simply can NOT delete a tail without the reference of the previous node.

  • 0

    I think it's the ' == null' check is fine. This way, if the user tries to delete the tail by accident, we would just ignore it instead of throwing a null pointer exception.

    Of course, if this was actual production code, it might be clearer to throw an IllegalArgumentException if the user did try to delete the tail so we wouldn't fail silently.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.