#include <iostream>
struct ListNode
{
int val;
ListNode *next;
ListNode(int x = 0) : val(x), next(NULL) {}
};
ListNode * RemoveDuplicated(ListNode *head)
{
if (head == NULL) return head;
ListNode** last = &head;
ListNode* p = head;
if (p->next == NULL || p->val != p->next->val)
{
*last = p;
last = &(p->next);
}
int val = p->val;
while((p = p->next) != NULL)
if (p->val != val)
{
if (p->next == NULL || p->val != p->next->val)
{
*last = p;
last = &(p->next);
}
val = p->val;
}
*last = NULL;
return head;
}
using namespace std;
void PrintList(ListNode* h)
{
while(h)
{
cout << h->val << ',';
h = h->next;
}
cout << endl;
}
int main(int argc, char** argv)
{
int A[] = {1,2,2,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10};
int n =sizeof(A)/sizeof(int);
ListNode L[n];
for (int i = 0; i < n-1; ++i)
{
L[i].val = A[i];
L[i].next = L+(i+1);
}
L[n-1].val = A[n-1];
PrintList(L);
PrintList(RemoveDuplicated(L));
return 0;
}