# Program to Remove Duplicate Elements From a Circular Linked List

Program to Remove Duplicate Elements From a Circular Linked List on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

## Q. Program to remove duplicate elements from a circular linked list.

### Explanation

In this program, we will create a circular linked list and remove duplicate nodes from the list. We will compare a node with rest of the list and check for the duplicate. If the duplicate is found, delete the duplicate node from the list.

1->2->2->4->3

In the above list, we can see, node 2 is present twice in the list. So, we will have a node current that will iterate through the list. The index will point to next node to current. Temp will be pointing to the node previous to index. When a duplicate is found, we delete it by pointing temp.next to index.next. Above list after removing duplicates:

1->2->4->3

### Algorithm

1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
2. Define another class for creating the circular linked list and it has two nodes: head and tail.
3. removeDuplicate() will remove duplicate nodes from the list:
1. Node current will point to head and used to traverse through the list.
2. The index will point to the next node to current and temp will point to previous node to index.
3. We will compare the current.data with the index.data. If the match is found, delete duplicate data by pointing temp's next to index's next.
4. Increment index to index.next and current to current .next.
5. Repeat step from c to d till all the duplicates are removed.

## Solution

### Python

```#Represents the node of list.
#Represents the node of list.
class Node:
def __init__(self,data):
self.data = data;
self.next = None;

class CreateList:
#Declaring head and tail pointer as null.
def __init__(self):
self.tail = Node(None);

#This function will add the new node at the end of the list.
newNode = Node(data);
#Checks if the list is empty.
#If list is empty, both head and tail would point to new node.
self.tail = newNode;
else:
#tail will point to new node.
self.tail.next = newNode;
#New node will become new tail.
self.tail = newNode;

#Removes duplicate from the list
def removeDuplicate(self):
print("List is empty");
else:
while(True):
#Temp will point to previous node of index.
temp = current;
#Index will point to node next to current
index = current.next;
#If current node is equal to index data
if(current.data == index.data):
#Here, index node is pointing to the node which is duplicate of current node
#Skips the duplicate node by pointing to next node
temp.next = index.next;
else:
#Temp will point to previous node of index.
temp = index;
index= index.next;
current =current.next;
break;

#Displays all the nodes in the list
def display(self):
print("List is empty");
return;
else:
#Prints each node by incrementing pointer.
print(current.data);
current = current.next;
print(current.data);
print("\n");

cl = CreateList();

print("Originals list: ");
cl.display();
#Removes duplicate nodes
cl.removeDuplicate();
print("List after removing duplicates: ");
cl.display();
```

Output:

```Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
```

### C

```#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//Represents the node of list.
struct node{
int data;
struct node *next;
};

//Declaring head and tail pointer as null.
struct node *tail = NULL;

//This function will add the new node at the end of the list.
//Create new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
tail = newNode;
}else {
//tail will point to new node.
tail->next = newNode;
//New node will become new tail.
tail = newNode;
}
}

//Removes duplicate from the list
void removeDuplicate() {
struct node *current = head, *index = NULL, *temp = NULL;
printf("List is empty");
}
else {
do{
//Temp will point to previous node of index.
temp = current;
//Index will point to node next to current
index = current->next;
//If current node is equal to index data
if(current->data == index->data) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
temp->next = index->next;
}
else{
//Temp will point to previous node of index.
temp = index;
}
index= index->next;
}
current =current->next;
}
}
//This function will display the nodes of circular linked list
void display(){
printf("List is empty");
}
else{
do{
//Prints each node by incrementing pointer.
printf("%d ", current->data);
current = current->next;
printf("\n");
}
}

int main()
{
printf("Originals list: \n");
display();
//Removes duplicate nodes
removeDuplicate();
printf("List after removing duplicates: \n");
display();

return 0;
}
```

Output:

```Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
```

### JAVA

```public class RemoveDuplicate {
//Represents the node of list.
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
//Declaring head and tail pointer as null.
public Node tail = null;
//This function will add the new node at the end of the list.
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
tail = newNode;
}
else {
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
}
}
//Removes duplicate from the list
public void removeDuplicate() {
Node current = head, index = null, temp = null;
System.out.println("List is empty");
}
else {
do{
//Temp will point to previous node of index.
temp = current;
//Index will point to node next to current
index = current.next;
//If current node is equal to index data
if(current.data == index.data) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
temp.next = index.next;
}
else {
//Temp will point to previous node of index.
temp = index;
}
index= index.next;
}
current =current.next;
}
}
//Displays all the nodes in the list
public void display() {
System.out.println("List is empty");
}
else {
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.data);
current = current.next;
System.out.println();
}
}
public static void main(String[] args) {
RemoveDuplicate cl = new RemoveDuplicate();
System.out.println("Originals list: ");
cl.display();
//Removes duplicate nodes
cl.removeDuplicate();
System.out.println("List after removing duplicates: ");
cl.display();
}
}
```

Output:

```Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
```

### C#

```using System;
{
public class Program
{
//Represents the node of list.
public class Node<T>{
public T data;
public Node<T> next;
public Node(T value) {
data = value;
next = null;
}
}
public class CreateList<T>{
protected Node<T> tail = null;
//This function will add the new node at the end of the list.
//Create new node
Node<T> newNode = new Node<T>(data);
//Checks if the list is empty.
tail = newNode;
}else{
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
}
}
//Removes duplicate from the list
public void removeDuplicate() {
Node<T> current = head, index = null, temp = null;
Console.WriteLine("List is empty");
}
else {
do{
//Temp will point to previous node of index.
temp = current;
//Index will point to node next to current
index = current.next;
//If current node is equal to index data
if(current.data.Equals(index.data)) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
temp.next = index.next;
}
else{
//Temp will point to previous node of index.
temp = index;
}
index= index.next;
}
current =current.next;
}
}
//Displays all the nodes in the list
public void display(){
Console.WriteLine("List is empty");
}
else{
do{
//Prints each node by incrementing pointer.
Console.Write(" "+ current.data);
current = current.next;
Console.WriteLine();
}
}
}
public static void Main()
{
CreateList<int> cl = new CreateList<int>();
Console.WriteLine("Originals list: ");
cl.display();
//Removes duplicate nodes
cl.removeDuplicate();
Console.WriteLine("List after removing duplicates: ");
cl.display();
}
}
}
```

Output:

```Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
```

### PHP

```<!DOCTYPE html>
<html>
<body>
<?php
//Represents the node of list.
class Node{
public \$data;
public \$next;
function __construct(\$data){
\$this->data = \$data;
\$this->next = NULL;
}
}
class CreateList{
//Declaring head and tail pointer as null.
private \$tail;
function __construct(){
\$this->tail = NULL;
}
//This function will add the new node at the end of the list.
//Create new node
\$newNode = new Node(\$data);
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
\$this->tail = \$newNode;
}
else{
//tail will point to new node.
\$this->tail->next = \$newNode;
//New node will become new tail.
\$this->tail = \$newNode;
}
}

//Removes duplicate from the list
function removeDuplicate() {
echo "List is empty";
}
else {
do{
//Temp will point to previous node of index.
\$temp = \$current;
//Index will point to node next to current
\$index = \$current->next;
//If current node is equal to index data
if(\$current->data == \$index->data) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
\$temp->next = \$index->next;
}
else {
//Temp will point to previous node of index.
\$temp = \$index;
}
\$index= \$index->next;
}
\$current = \$current->next;
}
}

//Displays all the nodes in the list
function display() {
echo "List is empty";
}
else {
do{
//Prints each node by incrementing pointer.
echo(" \$current->data");
\$current = \$current->next;
echo "<br>";
}
}
}
\$cl = new CreateList();

echo "Originals list:<br>";
\$cl->display();
//Removes duplicate nodes
\$cl->removeDuplicate();
echo "List after removing duplicates:<br>";
\$cl->display();
?>
</body>
</html>
```

Output:

```Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
```

Next Topic#