This problem is wrong in C language


  • 1
    P

    someone says this answer can be accepted,

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

    but, what will happen if the node is the last node of the list?

    you are setting node->val = NULL->val, it will crash when run!

    this could be avoid anyway by checking the value.


    But the really trouble is, you didn't set "node->pre->next = NULL"!!!!!!!!!!!!!

    you can not do this if you can't get the pre node, so this problem is wrong !

    you should never code like this in real world !!!!!


  • 0
    P

    ok, i didn't see the statement "except the tail"..................


  • 0
    V

    you can just check the node at the beginning,like this:
    if (!node || !node->next) return;


Log in to reply
 

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