[C++] | Detailed explanation w/ Actually Memory delete| Commented Code | Easy to understand
Brief note about Question-\n\nLet\'s take an example not given-\nSuppose our head given to us as head = -1 -> 0 -> 3 -> 4 -> 4 -> 4 -> 5 -> 5-> 6\n\nSince, 4 an
***Brief note about Question-***\n```\nLet\'s take an example not given-\nSuppose our head given to us as head = -1 -> 0 -> 3 -> 4 -> 4 -> 4 -> 5 -> 5-> 6\n\nSince, 4 and 5 are repeating, so we delete that nodes, and our final answer looks like\nhead = -1 -> 0 -> 3 -> 6\n```\n_____________\n***Solution - I (Accepted)-***\n* First we create a dummy node name as `prehead` whose next contains the` head` node.\n* After that, we have to make a current pointer and moves that pointer in the our linked list.\n* Suppose at  a point we find `curr ->next -> val == curr -> next -> next -> val`then we have to delete all nodes upto which they are having the same value.\n* We actually delete that nodes from our linked list and free up memory.\n* And lastly, as use of dummy node i.e our` prehead`, it\'s next store our head node, so we return `prehead -> next.`\n* Since, It is pretty straightforward to visuliaze the links how they works here, that\'s why in this post i don\'t include images.\n* See code for more clarification.\n________\n**Code (C++)**\n```\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    ListNode* deleteDuplicates(ListNode* head) {\n        //if head is NULL or just having a single node, simply return from here head \n        if(head == NULL || head->next == NULL) \n            return head;\n        \n        // creating our dummy node i.e prehead\n        // we are giving it\'s value is -101, because see constraints\n        // -100 <= Node.val <= 100\n        ListNode* prehead = new ListNode(-101);\n        \n        prehead -> next = head; // prehead\'s next contain head\n        \n        ListNode* curr = prehead; // make a current pointer to move\n        \n        // while it not reaches to the end\n        while(curr -> next != NULL && curr -> next -> next != NULL)\n        {\n            // if values are equal, now we have to delete values, till when they are equal\n           if(curr -> next -> val == curr -> next -> next -> val)\n           {\n               // temp pointer to find upto when the va;ues are equal and delete that nodes which ar not be include in our answer\n               ListNode* temp = curr -> next -> next;\n               while(temp != NULL && curr -> next -> val == temp -> val)\n               {\n                   ListNode *anthortemp = temp;\n                   temp = temp -> next;\n                   delete anthortemp; // actually freeing up the memory\n               }\n               curr -> next = temp;\n           }\n           else // if values are not equal\'s then simply move curr to next\n           {\n               curr = curr -> next;\n           }\n        }\n        \n        // and at last, prehead\'s next contain head node, so return that\n        return prehead -> next;\n        \n    }\n};\n```\n***`If u find this useful , please consider to give a upvote!!`***
['C']