Little trick with vivid explanation.


  • 1

    We just need to make the node that we want to delete equals the next node:
    For example, we want to delete node 2.
    Before:0_1474445941591_QQ截图20160921161029.bmp
    After: node2 stores everything of node3 now0_1474445969196_QQ截图20160921161116.bmp

    void deleteNode(struct ListNode* node) {
        if(node->next == NULL) return;
        node->val = node->next->val;
        node->next = node->next->next;
    }
    

  • 0

    That is smart!


Log in to reply
 

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