Very Easy 100% (Fully Explained)(Java, C++, Python, JS, C, Python3)
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked li
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.\n# **Java Solution:**\nRuntime: 1 ms, faster than 93.57% of Java online submissions for Remove Duplicates from Sorted List II.\n```\nclass Solution {\n    public ListNode deleteDuplicates(ListNode head) {\n        // Special case...\n        if (head == null || head.next == null)\n            return head;\n        // create a fake node that acts like a fake head of list pointing to the original head and it points to the original head......\n        ListNode fake = new ListNode(0);\n        fake.next = head;\n        ListNode curr = fake;\n        // Loop till curr.next and curr.next.next not null\n        while(curr.next != null && curr.next.next != null){         // curr.next means the next node of curr pointer and curr.next.next means the next of next of curr pointer...\n            // if the value of curr.next and curr.next.next is same...\n            // There is a duplicate value present in the list...\n            if(curr.next.val == curr.next.next.val) {\n                int duplicate = curr.next.val;\n                // If the next node of curr is not null and its value is eual to the duplicate value...\n                while(curr.next !=null && curr.next.val == duplicate) {\n                    // Skip those element and keep updating curr...\n                    curr.next = curr.next.next;\n                }\n            }\n            // Otherwise, move curr forward...\n            else{\n                curr = curr.next;\n            }\n        }\n        return fake.next;       // Return the linked list...\n    }\n}\n```\n\n# **C++ Solution:**\nRuntime: 6 ms, faster than 88.10% of C++ online submissions for Remove Duplicates from Sorted List II.\n```\nclass Solution {\npublic:\n    ListNode* deleteDuplicates(ListNode* head) {\n        // create a fake node that acts like a fake head of list pointing to the original head...\n        ListNode* fake = new ListNode(0);\n        // fake node points to the original head...\n        fake->next = head;\n        ListNode* pre = fake;     //pointing to last node which has no duplicate...\n        ListNode* curr = head;      // To traverse the linked list...\n        // Now we traverse nodes and do the process...\n        while (curr != NULL) {\n            // Create a loop until the current and previous values are same, keep updating curr...\n            while (curr->next != NULL && pre->next->val == curr->next->val)\n                curr = curr->next;\n            // if curr has non-duplicate value, move the pre pointer to next node...\n            if (pre->next == curr)\n                pre = pre->next;\n            // If curr is updated to the last duplicate value, discard it & connect pre and curr->next...\n            else\n                pre->next = curr->next;\n                // Move curr forward...\n                // In next iteration, we still need to check whether curr points to duplicate value...\n                curr = curr->next;\n        }\n        // Return the linked list...\n        return fake->next;\n    }\n};\n```\n\n# **Python Solution:**\nRuntime: 28 ms, faster than 79.86% of Python online submissions for Remove Duplicates from Sorted List II.\n```\nclass Solution(object):\n    def deleteDuplicates(self, head):\n        fake = ListNode(-1)\n        fake.next = head\n        # We use prev (for node just before duplications begins), curr (for the last node of the duplication group)...\n        curr, prev = head, fake\n        while curr:\n            # while we have curr.next and its value is equal to curr...\n            # It means, that we have one more duplicate...\n            while curr.next and curr.val == curr.next.val:\n                # So move curr pointer to the right...\n                curr = curr.next\n            # If it happens, that prev.next equal to curr...\n            # It means, that we have only 1 element in the group of duplicated elements...\n            if prev.next == curr:\n                # Don\'t need to delete it, we move both pointers to right...\n                prev = prev.next\n                curr = curr.next\n            # Otherwise, we need to skip a group of duplicated elements...\n            # set prev.next = curr.next, and curr = prev.next...\n            else:\n                prev.next = curr.next\n                curr = prev.next\n        # Return the linked list...\n        return fake.next\n```\n        \n# **JavaScript Solution:**\nRuntime: 91 ms, faster than 67.42% of JavaScript online submissions for Remove Duplicates from Sorted List II.\n```\nvar deleteDuplicates = function(head) {\n    // Special case...\n    if (head == null || head.next == null)\n        return head;\n    // create a fake node that acts like a fake head of list pointing to the original head and it points to the original head......\n    var fake = new ListNode(0);\n    fake.next = head;\n    var curr = fake;\n    // Loop till curr.next and curr.next.next not null\n    while(curr.next != null && curr.next.next != null){         // curr.next means the next node of curr pointer and curr.next.next means the next of next of curr pointer...\n        // if the value of curr.next and curr.next.next is same...\n        // There is a duplicate value present in the list...\n        if(curr.next.val == curr.next.next.val) {\n            let duplicate = curr.next.val;\n            // If the next node of curr is not null and its value is eual to the duplicate value...\n            while(curr.next !=null && curr.next.val == duplicate) {\n                // Skip those element and keep updating curr...\n                curr.next = curr.next.next;\n            }\n        }\n        // Otherwise, move curr forward...\n        else{\n            curr = curr.next;\n        }\n    }\n    return fake.next;       // Return the linked list...\n};\n```\n\n# **C Language:**\n```\nstruct ListNode* deleteDuplicates(struct ListNode* head){\n    // create a fake node that acts like a fake head of list pointing to the original head...\n    struct ListNode* fake = (struct ListNode*)malloc(sizeof(struct ListNode));\n    // fake node points to the original head...\n    fake->next = head;\n    struct ListNode* pre = fake;        //pointing to last node which has no duplicate...\n    struct ListNode* curr = head;       // To traverse the linked list...\n    // Now we traverse nodes and do the process...\n    while (curr != NULL) {\n        // Create a loop until the current and previous values are same, keep updating curr...\n        while (curr->next != NULL && pre->next->val == curr->next->val)\n            curr = curr->next;\n        // if curr has non-duplicate value, move the pre pointer to next node...\n        if (pre->next == curr)\n            pre = pre->next;\n        // If curr is updated to the last duplicate value, discard it & connect pre and curr->next...\n        else\n            pre->next = curr->next;\n            // Move curr forward...\n            // In next iteration, we still need to check whether curr points to duplicate value...\n            curr = curr->next;\n    }\n    // Return the linked list...\n    return fake->next;\n}\n```\n\n# **Python3 Solution:**\n```\nclass Solution:\n    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:\n        fake = ListNode(-1)\n        fake.next = head\n        # We use prev (for node just before duplications begins), curr (for the last node of the duplication group)...\n        curr, prev = head, fake\n        while curr:\n            # while we have curr.next and its value is equal to curr...\n            # It means, that we have one more duplicate...\n            while curr.next and curr.val == curr.next.val:\n                # So move curr pointer to the right...\n                curr = curr.next\n            # If it happens, that prev.next equal to curr...\n            # It means, that we have only 1 element in the group of duplicated elements...\n            if prev.next == curr:\n                # Don\'t need to delete it, we move both pointers to right...\n                prev = prev.next\n                curr = curr.next\n            # Otherwise, we need to skip a group of duplicated elements...\n            # set prev.next = curr.next, and curr = prev.next...\n            else:\n                prev.next = curr.next\n                curr = prev.next\n        # Return the linked list...\n        return fake.next\n```\n**I am working hard for you guys...\nPlease upvote if you find any help with this code...**
['C', 'Python', 'Java', 'Python3', 'JavaScript']