Solution By Dare2Solve | Detailed Explanation | Clean Code
Explanation []\nauthorslog.vercel.app/blog/aE9Z86FRct\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    ListNode* deleteDuplicates(ListNode* head) {\n        
```Explanation []\nauthorslog.vercel.app/blog/aE9Z86FRct\n```\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    ListNode* deleteDuplicates(ListNode* head) {\n        if (!head || !head->next) {\n            return head;\n        }\n        ListNode* output = new ListNode(0, head);\n        ListNode* pre = output;\n        \n        while (head) {\n            if (head->next && head->val == head->next->val) {\n                while (head->next && head->val == head->next->val) {\n                    head = head->next;\n                }\n                pre->next = head->next;\n            } else {\n                pre = pre->next;\n            }\n            head = head->next;\n        }\n        \n        return output->next;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def deleteDuplicates(self, head: ListNode) -> ListNode:\n        if not head or not head.next:\n            return head\n        \n        output = ListNode(0, head)\n        pre = output\n        \n        while head:\n            if head.next and head.val == head.next.val:\n                while head.next and head.val == head.next.val:\n                    head = head.next\n                pre.next = head.next\n            else:\n                pre = pre.next\n            head = head.next\n        \n        return output.next\n```\n\n```java []\nclass Solution {\n    public ListNode deleteDuplicates(ListNode head) {\n        if (head == null || head.next == null) {\n            return head;\n        }\n        \n        ListNode output = new ListNode(0, head);\n        ListNode pre = output;\n        \n        while (head != null) {\n            if (head.next != null && head.val == head.next.val) {\n                while (head.next != null && head.val == head.next.val) {\n                    head = head.next;\n                }\n                pre.next = head.next;\n            } else {\n                pre = pre.next;\n            }\n            head = head.next;\n        }\n        \n        return output.next;\n    }\n}\n```\n\n```javascript []\nvar deleteDuplicates = function(head) {\n    if(!head || !head.next) {\n        return head\n    }\n    let output = new ListNode(0, head), pre = output\n    while(head) {\n        if(head.next && head.val === head.next.val) {\n            while(head.next && head.val === head.next.val) {\n                head = head.next\n            }\n            pre.next = head.next\n        } else {\n            pre = pre.next\n        }\n        head = head.next\n    }\n    return output.next    \n};\n```
['Linked List', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']