Easy solution in java


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

    Since we couldn't enter the preceding node, we can not delete the given node. We can just copy the next node to the given node and delete the next one.


  • -3
    Y

    Release the memory:

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

  • 0
    Y

    The 'temp' is unnecessary. Since I set node.next=node.next.next, that means the next node will not have reference, then the Java System will delete the node without reference automatically.


  • 0
    K

    Yep, garbage collection


  • 0
    D

    great idea! Thanks


  • 0
    H
    This post is deleted!

  • 1
    J

    null pointer


  • 5
    T

    If node.next==null; I think the " node.next.value" is invalid.


  • 0
    S

    @tjuli The given node won't be the tail, so node.next != null;


  • 0
    S

    Do we have more faster solution with java than this? I submitted the same solution and the cost time is only 1ms, however, only beat 2.67% java solutions, why?


  • 0
    M

    Question: How is this a viable solution? You are simply changing the current and next node values. Shouldn't the answer also shorten the entire linked list? For example, if you wanted to delete the '1' in 1 -> 2 -> 3 -> 4, this algorithm would yield 2 -> 3 -> 3 -> 4. This isn't deleting the node, it is just overwriting one of the nodes in the list. Shouldn't the correct result be 2 -> 3 -> 4?


  • 0
    S

    @tjuli The question says "except the tail", so "node.next.val" is valid.


  • 0
    S

    @michael216 For your example, this algorithm actually yields 2->3->4. By using "node.next = node.next.next", your "2" should be point to the second "3" now. You don't have reference to the first "3" any more.


  • 1
    T

    Here's my solution.

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

  • 0
    C

    @yangneu2015
    Hello,
    I just have a question that if we have 1 - 2 - 3 - 4 four nodes in the list, if I want to delete Node(3) node.next = node.next.next Should point to "null" .Why there are no nullpointer exception occurs? I am just a beginner. Thank you.


  • 0
    V

    @trimal .. same as my solution...


  • 0
    Y

    @tjuli Please read the question again


  • 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
    L
    This post is deleted!

  • 0
    L

    @spectrojin I think this might be the issue, when you use

    node=node.next
    

    it doesn't mean you copied the node.next to node, it means that the reference of the first node is now pointed to node.next,therefore, the node right now has no unique reference, and it would be garbage collected, to simplify, I think what this line of code probably means this:whenever you access node, you actually access node.next I just learnt this too, not so sure, please point out if I am wrong.


Log in to reply
 

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