# LC 83. Remove Duplicates from Sorted List ### [Problem link](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) ###### tags: `leedcode` `easy` `c++` `Linked List` Given the <code>head</code> of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list **sorted** as well. **Example 1:** <img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/list1.jpg" style="width: 302px; height: 242px;" /> ``` Input: head = [1,1,2] Output: [1,2] ``` **Example 2:** <img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/list2.jpg" style="width: 542px; height: 222px;" /> ``` Input: head = [1,1,2,3,3] Output: [1,2,3] ``` **Constraints:** - The number of nodes in the list is in the range <code>[0, 300]</code>. - <code>-100 <= Node.val <= 100</code> - The list is guaranteed to be **sorted** in ascending order. ## Solution 1 #### C++ ```cpp= /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if (head == nullptr) { return nullptr; } ListNode *cur = head; while (cur->next) { if (cur->val == cur->next->val) { cur->next = cur->next->next; } else { cur = cur->next; } } return head; } }; ``` >### Complexity >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(n) | O(1) | ## Note x