1-3 lines, C++/Java/Python/C/C#/JavaScript/Ruby


  • 123

    We can't really delete the node, but we can kinda achieve the same effect by instead removing the next node after copying its data into the node that we were asked to delete.

    C++

    void deleteNode(ListNode* node) {
        *node = *node->next;
    }
    

    But better properly delete the next node:

    void deleteNode(ListNode* node) {
        auto next = node->next;
        *node = *next;
        delete next;
    }
    

    Java and C#

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

    Python

    def deleteNode(self, node):
        node.val = node.next.val
        node.next = node.next.next
    

    C

    void deleteNode(struct ListNode* node) {
        *node = *node->next;
    }
    

    But better properly free the next node's memory:

    void deleteNode(struct ListNode* node) {
        struct ListNode* next = node->next;
        *node = *next;
        free(next);
    }
    

    JavaScript

    var deleteNode = function(node) {
        node.val = node.next.val;
        node.next = node.next.next;
    };
    

    Ruby

    def delete_node(node)
        node.val = node.next.val
        node.next = node.next.next
        nil
    end

  • 1

    Hi, Stefan. The 1-line code *node = *node -> next is really interesting. I have runned it on some examples and I can see that it works. But I am just not very clear what the linked list is like after running this code. Suppose the list is 1 -> 2 -> 3 -> 4 and node is 3, then the final linked list is 1 -> 2 -> 4. But since there is no delete operation, so I wonder whether there is another list 3 -> 4 with 3 as the head and intersects with the former list at node 4? Is that right? Thanks!

    BTW, it is also interesting to note that codes in Java and C# are just the same :-)


  • 4

    But since there is no delete operation, so I wonder whether there is another list 3 -> 4 with 3 as the head and intersects with the former list at node 4? Is that right? Thanks!

    Not quite. If I'm not mistaken, *node = *node->next just copies all data, so it's equivalent to this:

    node->val = node->next->val;
    node->next = node->next->next;
    

    So in your example, besides the 1->2->4 list, there's the original node with value 4 being all alone now. It is lost and its memory is only freed by the operating system when the program exits (or possibly by the OJ if it keeps extra pointers). If instead we were to delete node 2, then you'd have the list 1->3->4 and the lost node would head the list 3->4, intersecting with the main list at 4.

    BTW, it is also interesting to note that codes in Java and C# are just the same :-)

    Yeah, they often are, at least for such simple problems using only basic coding.


  • 2

    I just noticed you wrote *node -> next, adding spaces around ->, suggesting * is stronger than ->. That's not the case. The -> is stronger. It is *(node->next), not (*node)->next.


  • 0

    Hi, Stefan. Thank you for your detailed explanations and the nice remind for the precedence of * and ->. I have got the idea now. :-)


  • -2
    K
    //cpp or C++ concise solution. Runs in 16ms  by ekapisa or skapil
    void deleteNode(ListNode* node) {
            ListNode *temp=NULL;
            node->val = (node->next)->val;
            temp =  node->next;
            node->next = (node->next)->next;
            if(temp)
            {
                delete temp;
            }
            
            
        }

  • 0
    M

    You C++ answer is interesting. But what happens if node==NULL or node->next==NULL? The code:

    auto next = node->next; // fails when node==NULL

    *node = *next; // fails when node->next==NULL since you are dereferencing a null pointer

    Please correct me if I am wrong, thanks.


  • 2

    @morrischen2008 Yes, that would of course fail. But we're tasked with deleting "a node (except the tail)". I interpret that as meaning that we'll never be asked to delete a tail node or even given a null pointer. And the OJ indeed doesn't include such inputs, which it should if they were allowed (edge cases should always be tested).


  • 0
    M

    It makes sense. Thanks!


  • 0
    J

    I think need check whether node is the last element or not, otherwise "node.next" throws NullPointerException. The safe java version should be

    void DeleteNode(ListNode node){
    if(node.next != null){
    node.val = node.next.val;
    node.next = node.next.next;
    }else{
    node = null;
    }
    }


  • 0

    @jimi@9 See morrischen2008's comment above and my response to it.


  • 0
    T

    Hey. Assume the original linked list is 1 -> 2 -> 3 -> 4 and I want to 'delete' 2 in order to get result 1 -> 3 -> 4. After 'deletion' trick is done, is it better/necessary to point Node 3 to NULL instead of still directly pointing to Node 4?


  • 0
    S

    awesome answer!Thank you very much


  • 1
    V

    Could someone explain why , if i try
    node=node.next
    it doesn't work. Doesn't it assign the reference 'node' now to a the next object then ?


  • 1

    Really mind-blowing answer, thanks for sharing!


  • 1
    S

    This might be a stupid question, but why can't we just say node = node.next instead of node.val = node.next.val? Doesn't the node just get copied over then? I tried that and got a wrong answer.


  • 5
    U

    Thinking about the same thing! Why can't I directly do node = node.next? Could anyone answer this?


  • 0
    B

    thanks for sharing! I have a question ,e.g. There is a Linked List 1->2->3->4, if we want to delete value 3,so
    the current node is 3.In your method ,node.value = node.next.value is correct,but node.next is 4,what is the node.next.next?I think node.next.next is null. BTW,value 3 is not the tail node.


  • -1
    S

    What's wrong with this solution? Please explain anybody.

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

  • 0
    R

    What if the given node is the last one? You going to get NullPointerException.


Log in to reply
 

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