Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

public class Solution { public void deleteNode(ListNode node) { if(node != null && node.next != null) { node.val = node.next.val; node.next = node.next.next; } } }

Great! But how about if the node is the last one? I think there needs " if (node.next == null) { node = null; } "

The question given a condition that delete node except the tail.

You cannot set node null in this function actually, because it is a local variable.

if(node != null && node.next != null), the answer with this condition.

Given the condition that the node to be deleted won't be the tail one, above solution can be simplified to :

public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; }

Can someone explain why a loop isn't needed to iterate through the List node and a simple if statement will suffice?

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