#include <iostream>
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode deleteDuplicates(ListNode head) {
ListNode* current = head;
while (current != nullptr && current->next != nullptr) {
if (current->val == current->next->val) {
ListNode* temp = current->next;
current->next = current->next->next;
delete temp;
} else {
current = current->next;
}
}
return head;
}
void printList(ListNode* head) {
ListNode* current = head;
while (current != nullptr) {
std::cout << current->val << " ";
current = current->next;
}
std::cout << std::endl;
}
int main() {
// Example usage
ListNode* head = new ListNode(1);
head->next = new ListNode(1);
head->next->next = new ListNode(2);
head->next->next->next = new ListNode(3);
head->next->next->next->next = new ListNode(3);
std::cout << "Original List: ";
printList(head);
head = deleteDuplicates(head);
std::cout << "List after removing duplicates: ";
printList(head);
// Clean up memory
while (head != nullptr) {
ListNode* temp = head;
head = head->next;
delete temp;
}
return 0;
}