Simple C++ solution

  • 3
    class Solution {
    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

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

  • 0

    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.