A 16ms solution,Cpp

  • 2
    class Solution {  public:
    void deleteNode(ListNode* node) {
        ListNode* front,*rear;
        //rear always points the next node of front
        front = node;
        rear = front->next;
        //move front and rear until rear points the last node;
        while(rear->next != NULL)
            front->val = rear->val;
            rear = rear->next;
            front = front->next;
        //The last time that rear puts its val into front
        front->val = rear->val;
        delete rear;
        front->next = NULL;
    }  };

  • 0

    厉害! 我想了半天终于明白了! Amazing! I finally figured it out after several guessing

  • 2

    Why not just copy the value of the next node to this node and delete the next node.

Log in to reply

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