C++ unsorted linked list.

Hello. I'm looking for some help.
My task is to find if there are any duplicate values inside unsorted linked list.
So what im trying to do here is, to compare the lists first element with all the other ones and so on. Each time it finds its duplicate, it just increases count value by one, just to know in the end how many duplicates are there. So the problem is:

Lets say, if the list contains 3 nodes with values: 1,2,3. How to make sure that in every single check it wont compare it to it self, like if 1 gets compared to 1,2,3, it only compares it with 2/3 not 1/2/3. I'm rly bad at explaining things, so excuse me about that... That would be the first thing.

Basically, i would like to know if there are any solutions how to improve my code or if there's a better way how to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void duplicates(node *first)
{
    node *pOne, *pTwo;
    int count = 0;
    for(pOne = first;pOne != NULL; pOne = pOne->next)
    {
        for(pTwo = first; pTwo != NULL; pTwo = pTwo->next)
        {
            if(pOne->number == pTwo->number)
            {
                count++;
            }
        }
        if(count >1)
           {
                cout<<"The number "<<pOne->number<< " repeats "<< count-1 << " times."<< endl;
                count = 0;
           }
    }
};
On line 9, add && pOne != pTwo to make sure you're not comparing a node to itself.
Topic archived. No new replies allowed.