Simple and clear explanation of recursive solution
Problem Statement:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/\n\n### How to  write a Recursive function?\nWe have to follow three steps
Problem Statement:[https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/](http://)\n\n### How to  write a Recursive function?\n*We have to follow three steps to write a*  **recursive code** \n1.  **Assumption:**\n* \t  Assumption is nothing but what do you want your function to do.\n2. **Main Logic:**\n*     Solving problem using sub problems\n3. **Base Condition:**\n*     When recursion should terminate \n\n**Before diving into the solution lets understand the question**:\n\tThey have given a Linked list, we have to remove all the occurences of an element whose frequency is more than one and return the new linkedlist.\n\t![image](https://assets.leetcode.com/users/images/3fae7b24-93ab-4cfd-b452-de689684fa2f_1621272982.2501419.png)\n\t\nIn the given linkedlist frequencies of 3 and 4 is 2, which is greater than 1, so we have to delete all the occurence of 3 and 4 then we have to return head node of new linked list.\n\n### Defining our three steps in recursion\n**Assumption:** Remove all the occurences of an element whose frequency is more than one and return the linkedlist.\n\n**Main Logic:** Here we have two cases for a node either to consider it or not in final linkedlist.\n*    **Case-1:** If we land on a node whose frequency is 1, then we should \n      consider it and this node should point to the **nextnode** in the linkedlist\n      whose frequency is 1, if we don\'t find any node then it should point to NULL.\n*   **Case-2:** If we land on a node whose frequency is more than 1, then \n\t  we have to ignore all the nodes of same value till we get **nextnode** whose \n\t  frequency is 1, if we don\'t find any node then it should point to NULL.\n\t  \n**BaseCondition**: \n*    If we are at **Last node** then there is no need to check its frequency (because we are at lastnode there will be no nodes after it, so it\'s frequency is one.) so we can simply return that node.\n*    If the head node is NULL then we have to return.\n\n### Recursive Solution:\n```\nListNode* recursiveDeletion(ListNode*head){\n\t\t//Base Conditions:\n\t\tif(head==NULL || head->next==NULL)\n\t\t\treturn head;\n\t\t\t\n\t\tbool flag=false //To check the given node is repeating or not.\n\t\tint headValue=head->val;\n\t\t\n\t\tListNode*temp=head->next; //temporary pointer for itearating till we get\n\t\t\t\t\t\t\t\t //a node whose data is not equal to headValue.\n\t\twhile(temp!=NULL && temp->next==headValue){\n\t\t\tflag=true;\n\t\t\ttemp=temp->next; //shift to next node \n\t\t}\n\t\t//Now temp pointer points to a node whose value is different from headValue.\n\t\t\n\t\t// MAIN LOGIC: Case-2 (frequency more than 1)\n\t\tif(flag==true){\n\t\t\treturn recursiveDeletion(temp);\n\t\t\t// we are not appending temp node to the previous node of head \n            //because we don\'t know whether temp node is repeating or not \n\t\t}\n\t\telse{\n\t\t\t//Case-1 ( frequency is 1)\n\t\t\thead->next= recursiveDeletion(head->next);\n\t\t\t// we are considering head in our linkedlist\n\t\t\t\n\t\t\treturn head;\n\t\t\t//******why should we return head?***********\n\t\t\t// beacuse we have to link head node to the prev node as we called\n\t\t\t//prev->next=recursiveDeletion(prev->next)  prev->next == head \n            //as head is not repeating we have to return pointer of head.\n\t\t\t\n\t\t\t//head points to the nextnode return by the function it can either be\n\t\t\t // NULL or a node\n\t\t\t\n\t\t}\t\n}\n```\n\nIf you have any doubts feel free to ask in comment section\n\nThis is my first article tell me your review on this and where can I improve.\n\n\n\n\t\n
['Recursion', 'C', 'C++']