A 16ms solution,Cpp


  • 2
    H
    class Solution {  public:
    void deleteNode(ListNode* node) {
        ListNode* front,*rear;
        //rear指向front后面一个结点
        //rear always points the next node of front
        front = node;
        rear = front->next;
        //front和rear同步后移,每次都把rear的值赋给front
        //一直将rear移动到最后一个结点
        //move front and rear until rear points the last node;
        while(rear->next != NULL)
        {
            front->val = rear->val;
            rear = rear->next;
            front = front->next;
        }
        //此时最后一次把rear的值赋值给front,rear就成了多余的那个结点。
        //The last time that rear puts its val into front
        front->val = rear->val;
        delete rear;
        front->next = NULL;
    }  };

  • 0
    A

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


  • 2
    L

    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.