Simple C++ solution


  • 3
    S
    class Solution {
    public:
    void deleteNode(ListNode* node) {
        if(node == NULL || node->next == NULL)    return;
        node->val = node->next->val;
        ListNode *next = node->next;
        node->next = node->next->next;
        delete next;
    }
    };

  • 0
    G

    your code cannot work when you prepare deleting the last node in the list.


  • 0
    S

    Yeah, you are right, however the problem has said that the function will delete a node (except the tail)


Log in to reply
 

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