Both the Solutions FULLY and CLEARLY Explained || 1. Iterative solution || 2. Using STL (MAP)
Intuition\n Describe your first thoughts on how to solve this problem. \n1. For iterative solution, simply iterate the Linked List and check for duplicate nodes
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. For iterative solution, simply iterate the Linked List and check for duplicate nodes and delete them.\n2. For STL solution, store the elements of the Linked List in the map and delete them if their frequency is greater than 1.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n***Solution 1:***\n\n1. Iterate the Linked List from one step back than the head, as it is possible that we need to delete the first element as well, and we know that **for deleting any node we require the previous node**.\n2. Now just iterate the Linked List and store the current value of node in a variable named \'variable\' and till the subsequent elements == varible delete them.\n3. If the above condition is not true, just move forward i.e., itr = itr -> next.\n4. Finally return prev -> next (since our head of the list is stored in prev -> next pointer).\n\n\n***Solution 2:***\n\n1. Iterate the Linked List and store the elements in a map named mp.\n2. Create a new node \'newHead\' as the dummy head of the resulting Linked List.\n3. Create a temporary pointer \'temp\' to keep treack of the last node in the resulting Linked List. Initalize it to \'newHead\'.\n4. Iterate over the map, when (it.second == 1) create a new node \'ans\' with the value (it.first).\n5. Connect the new node \'ans\' to the last node in the resulting Linked List by assigning \'temp -> next = ans\', and update \'temp\' to \'ans\' for the next iteration.\n6. After processing all the unique values, the resulting Linked List is ready. Return \'newHead -> next\', which points to the first node in the resulting Linked List. \n\n# Complexity\n- Time complexity: **O(n)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nTime complexity is O(n) for both the approaches.\n\n- Space complexity: \n**Solution 1: O(1)\n Solution 2: O(n)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nIn Solution 2 we are using map to store the unique values of the Linked List.\n\n# Code\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        \n        if(head == NULL) return head;\n\n        ListNode* prev = new ListNode(); //this is pointing before head\n        //Let\'s say the list is 2 -> 2 -> 3 -> 5 -> 5 -> 5, in this case we have to delete the 1st element as well, therefore we have used prev pointer\n        prev -> next = head;\n\n        ListNode* itr = prev;\n\n        while(itr -> next != NULL && itr -> next -> next != NULL)\n        {\n            if(itr -> next -> val == itr -> next -> next -> val)\n            {\n                int variable = itr -> next -> val;\n                //We have to delete all the duplicate elements along with the original element\n                while(itr -> next != NULL && itr -> next -> val == variable)\n                {\n                    itr -> next = itr -> next -> next;\n                }\n            }\n            else //if we\'ll not write else condition, we\'ll get TLE\n            {\n                itr = itr -> next;\n            }\n        }\n        return prev -> next; //head pointer is in prev -> next\n    }\n};\n```\n\n\n```\nclass Solution {\npublic:\n    ListNode* deleteDuplicates(ListNode* head) {\n        map <int, int> mp;\n\n        while(head != NULL)\n        {\n            mp[head -> val]++;\n            head = head -> next;\n        }\n\n        ListNode* newHead = new ListNode(); //Creating new list for answer\n        ListNode* temp = newHead; //temp variable to keep track of the last node\n        for(auto & it: mp)\n        {\n            if(it.second == 1)\n            {\n                ListNode* ans = new ListNode(it.first); //to avoid integer to pointer conversion\n                temp -> next = ans;\n                temp = ans;\n            }\n        }\n        return newHead -> next;\n    }\n};\n```
['Hash Table', 'Linked List', 'Python', 'C++', 'Java']