2 Line Java O(1) Fast Accepted Solution


  • 10
    M

    Since the node is guarentee not to be the tail, the following code works. Move the value and skip the next node.

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

  • 0
    U

    I am not sure this is complete, as the preceding node is not considered, for example, how to change the 'next' value of the preceding node?


  • 0
    L

    Since the preceding node is pointing to the given node, you can modify the current node and the preceding node will still be pointing to it.

    Above, all the poster did was change the val and next attributes of the node.


  • 0
    M

    Actually I don't like this way either. But it's impossible to truly delete one node from a list only given that node ( its preceding node is a must to truly remove that node from the list). So based on this problem, to move value is only we can do~~ Don't take this problem too serious I have to say.


  • 0
    A
    This post is deleted!

  • 0
    U

    Thank you! I understand now.


  • 0
    M

    I ran this code but its runtime only beats 2.72%.


Log in to reply
 

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