83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
ListNode* deleteDuplicates(ListNode* head) { // time: O(n); space: O(1)
ListNode* cur = head;
while (cur && cur->next) {
if (cur->val == cur->next->val) {
ListNode *nodeToDel = cur->next;
cur->next = cur->next->next;
delete nodeToDel;
} else {
cur = cur->next;
}
}
return head;
}
Last updated
Was this helpful?