/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if (head == NULL) return head; ListNode* p = head; while (p->next) { if (p->next->val == p->val) { ListNode* t = p->next; p->next = t->next; delete t; } else p = p->next; } return head; } };