using
System;
using
System.Collections.Generic;
class
GFG
{
class
Node
{
public
int
data;
public
Node next;
public
Node(
int
d)
{
data = d;
next =
null
;
}
};
static
Node insert(Node head,
int
item)
{
Node temp =
new
Node(0);
temp.data = item;
temp.next = head;
head = temp;
return
head;
}
static
void
replaceDuplicates( Node head)
{
Dictionary<
int
,
int
> mymap =
new
Dictionary<
int
,
int
>();
Node temp = head;
int
maxNum = 0;
while
(temp !=
null
)
{
if
(mymap.ContainsKey(temp.data))
mymap[temp.data] = mymap[temp.data] + 1;
else
mymap.Add(temp.data, 1);
if
(maxNum < temp.data)
maxNum = temp.data;
temp = temp.next;
}
while
(head !=
null
)
{
if
(mymap[head.data] > 1)
mymap[head.data] = -1;
else
if
(mymap[head.data] == -1)
head.data = ++maxNum;
head = head.next;
}
}
static
void
printList( Node node)
{
while
(node !=
null
)
{
Console.Write(
"{0} "
, node.data);
node = node.next;
}
}
public
static
void
Main(String []args)
{
Node head =
new
Node(1);
head.next =
new
Node(3);
head.next.next =
new
Node(1);
head.next.next.next =
new
Node(4);
head.next.next.next.next =
new
Node(4);
head.next.next.next.next.next =
new
Node(2);
head.next.next.next.next.next.next =
new
Node(1);
Console.WriteLine(
"Linked list before"
+
" replacing duplicates"
);
printList(head);
replaceDuplicates(head);
Console.WriteLine(
"\nLinked list after"
+
" replacing duplicates"
);
printList(head);
}
}