✅ C++ || Easy To Understand || Recursion || Simple
82. Remove Duplicates from Sorted List II\nKNOCKCAT\n\n\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. Linked list
# 82. Remove Duplicates from Sorted List II\n**KNOCKCAT**\n\n```\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. Linked list problem, Recursion.\n5. Please Upvote if it helps\u2B06\uFE0F\n6. Link to my Github Profile contains a repository of Leetcode with all my Solutions. \u2B07\uFE0F\n\t// \uD83D\uDE09If you Like the repository don\'t foget to star & fork the repository\uD83D\uDE09\n```\n[LeetCode](https://github.com/knockcat/Leetcode)       **LINK TO LEETCODE REPOSITORY**\n``` ```\n\nPlease upvote my comment so that i get to win the 2022 giveaway and motivate to make such discussion post.\n**Happy new Year 2023 to all of you**\n**keep solving keep improving**\nLink To comment\n[Leetcode Give away comment](https://leetcode.com/discuss/general-discussion/2946993/2022-Annual-Badge-and-the-Giveaway/1734919)\n\n**EXPLANATION**\n\n* **Traverse the list** from the **head** (or start) node.\n* While traversing, **compare each node with its next node.**\n* If the **data of the next node is the same as the current node** then **delete the next node.** \n* **Before we delete a node**, we need to store the next pointer of the node.\n\n``` ```\n\n**TIME COMPLEXITY**  : O(N)  , *N is number of Nodes*\n**SPACE COMPLEXITY** : O(1) \n``` ```\n\n**CODE WITH EXPLANATION**\n```\nclass Solution {\npublic:\n    ListNode* deleteDuplicates(ListNode* head) {\n\t\t// if list contains no node\n        if(!head)\n            return 0;\n\t\t//if list containg a single node return head\n        if(!head -> next)\n            return head;\n        \n\t\t// keepitn val of head in a temp variable var.\n        int val = head ->val;\n\t\t// temp contain the reference of next  node\n        ListNode *temp = head -> next;\n        \n\t\t// if the next node have different value then,\n\t\t// update head->next by temp(temp contains reference to next node)\n        if(temp -> val != val)\n        {\n            head -> next = deleteDuplicates(temp);\n            return head;\n        }\n        else\n        {\n\t\t\t// if the values are same just update temp to temp->next;\n           while(temp && temp -> val == val)\n            {\n\t\t\t\t// assigning temp to p \n                ListNode *p = temp;\n\t\t\t\t// updating temp with next node\n                temp = temp->next;\n\t\t\t\t// freeing the memory\n                delete p;\n            }\n            return deleteDuplicates(temp);\n        }\n    }\n};\n```
['Linked List', 'Recursion', 'C', 'C++']