1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#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; //for empty list, return NULL

    ListNode * last = head;
    ListNode * p = head;
    int val = p->val;

    while((p = p->next) != NULL)
        if (val != p->val)
        {
            last->next = p;
            val = p->val;
            last = p;
        }

    last->next = 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,1,1,1,1,1,2,2,3,3,3,3,3,3,4,5,6,7,8,8,8,9,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;
}
View Program Text


Test Status