# Replace nodes with duplicates in linked list

Given a linked list that contains some random integers from 1 to n with many duplicates. Replace each duplicate element that is present in the linked list with the values n+1, n+2, n+3 and so on(starting from left to right in the given linked list).

Examples:

```Input : 1 3 1 4 4 2 1
Output : 1 3 5 4 6 2 7
Replace 2nd occurrence of 1 with 5 i.e. (4+1)
2nd occurrence of 4 with 6 i.e. (4+2)
3rd occurrence of 1 with 7 i.e. (4+3)

Input : 1 1 1 4 3 2 2
Output : 1 5 6 4 3 2 7
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :
1. Traverse the linked list, store the frequencies of every number present in linked list in a map and alongwith it find the maximum integer present in linked list i.e. maxNum.
2. Now, traverse the linked list again and if the frequency of any number is more than 1, set its value to -1 in map on its first occurrence.
3.The reason for this is that on next occurrence of this number we will find its value -1 which means this number has occurred before, change its data with maxNum + 1 and increment maxNum.

Below is the implementation of idea.

```// C++ Program to replace duplicates
// in an unsorted linked list
#include <bits/stdc++.h>
using namespace std;

/* A linked list node */
struct Node {
int data;
struct Node* next;
};

// Utility function to create a new Node
struct Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}

// Function to replace duplicates from a
{
// map to store the frequency of numbers
unordered_map<int, int> mymap;

// variable to store the maximum number
int maxNum = 0;

// traverse the linked list to store
// the frequency of every number and
// find the maximum integer
while (temp) {
mymap[temp->data]++;
if (maxNum < temp->data)
maxNum = temp->data;
temp = temp->next;
}

// Traverse again the linked list

// Mark the node with frequency more
// than 1 so that we can change the
// 2nd occurence of that number

// -1 means number has occurred
// before change its value

}
}

/* Function to print nodes in a given
void printList(struct Node* node)
{
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
cout << endl;
}

/* Driver program to test above function */
int main()
{
/* The constructed linked list is:
1->3->1->4->4->2->1*/
newNode(4);
next = newNode(2);
next->next = newNode(1);

cout << "Linked list before replacing"
<< " duplicates\n";

cout << "Linked list after replacing"
<< " duplicates\n";

return 0;
}
```

Output:

```Linked list before replacing duplicates
1 3 1 4 4 2 1
1 3 5 4 6 2 7
```

This article is contributed by Chhavi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
0 Average Difficulty : 0/5.0