/**
* 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)
{
ListNode* p = head;
ListNode* q = p->next;
while (q)
{
if (p->val == q->val){
p->next = q->next;
free(q);
q = p->next;
}
else{
p = p->next;
q = q->next;
}
} //while
}
return head;
*/
if (!head) return head;
ListNode* p1 = head;
ListNode* p2 = head;
while(p1)
{
p2 = p2->next;
while(p2 && p1->val == p2->val)
{
ListNode* tmp = p2->next;
//free(p2);
p2 = tmp;
}
p1->next = p2;
p1 = p2;
}
return head;
}
};