What's wrong with this solution?


  • 0
    S

    Folks, What's wrong with this solution?? Need explaination. Thanks.

        public static void deleteNode(ListNode node) {
            while (node.next != null){
                node.val = node.next.val;
                node = node.next;
            } node = null;
        }

  • 0
    G

    Making "node" to be null actually did nothing to your list. node is a just reference to you list node, by setting it to null only affect this reference itself.
    Only way to do so is manipulating .next from previous node.

    Following code works:

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

Log in to reply
 

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