# Eliminate duplicates from Linked List using C++ program

Eliminate duplicates from Linked List: In this tutorial, we will learn how to eliminate duplicate elements/nodes from the linked list using a C++ program? By Indrajeet Das Last updated : August 01, 2023

## Problem statement

Given a sorted linked list (elements are sorted in ascending order). Eliminate duplicates from the given LL, such that output LL contains only unique elements.

In this question, we are given a sorted linked list with duplicate elements in it. Our task is to remove the duplicate nodes of the linked list. Since the list is sorted, the duplicate elements will be present in consecutive orders, that makes the work a lot easier.

## Example

```Input format: Linked list elements
(separated by space and terminated by 1)

Sample Input 1 :
1 2 3 3 3 4 4 5 5 5 7 -1

Sample Output 1 :
1 2 3 4 5 7

Lets the list be:
1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 4 -> NULL
The modified list will be:
1 -> 2 -> 3 -> 4 -> NULL
```

## Eliminating duplicates from Linked List

to eliminate duplicates from Linked List, keep the temp pointer pointing to node while(temp -> next != NULL), check if the data of temp is equal to data of temp -> next. If they are equal, then point the temp -> next to temp -> next -> next.

```Original LL:  3 ->  3 ->  4 ->  ...
After Change: 3 ->  4 ->  ... //Leaving out the middle 3 ->
```

This will keep on until we reached the end of the list and return head at the end.

Let's understand these steps with an example -

```1) 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 4 -> NULL, temp = 1
2) 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 4 -> NULL, temp = 2
3) 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 4 -> NULL, temp = 3
4) 1 -> 2 -> 3 -> 4 -> 4 -> 4 -> NULL, temp = 4
5) 1 -> 2 -> 3 -> 4 -> 4 -> NULL, temp = 4

Finally,
1 -> 2 -> 3 -> 4 -> NULL
```

## Function / Pseudo Code

```Node *removeDuplicate(Node* head){
while(temp -> next != NULL && temp != NULL){
//Duplicate Found
if(temp -> data == temp->next->data){
//Duplicate Removed
temp  ->  next = temp  -> next  -> next;
}
else{
//No Duplicate Present
temp = temp -> next;
}
}
}
```

## C++ program to remove/eliminate duplicates from a linked list

```#include <bits/stdc++.h>
using namespace std;

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

Node* newNode(int k)
{ //defining new node
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = k;
temp->next = NULL;
return temp;
}

//Used to add new node at the end of the list
{
}
else {
Node* node = newNode(k);
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = node;
}

}

Node* createNewLL()
{
int cont = 1;
int data;
while (cont) {
cout << "Enter the data of the Node" << endl;
cin >> data;
cout << "Do you want to continue?(0/1)" << endl;
cin >> cont;
}
}

{
}
cout << "NULL" << endl;
}

//Function
{
while (temp->next != NULL && temp != NULL) {
//Duplicate Found
if (temp->data == temp->next->data) {
//DUplicate Removed
temp->next = temp->next->next;
}
else {
//No Duplicate Present
temp = temp->next;
}
}
}

// Main code
int main()
{

cout << "The linked list is" << endl;

cout << "The new Linked List is" << endl;

return 0;
}
```

## Output

```Enter the data of the Node
1
Do you want to continue?(0/1)
1
Enter the data of the Node
1
Do you want to continue?(0/1)
1
Enter the data of the Node
2
Do you want to continue?(0/1)
1
Enter the data of the Node
2
Do you want to continue?(0/1)
1
Enter the data of the Node
3
Do you want to continue?(0/1)
1
Enter the data of the Node
4
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
0
1->1->2->2->3->4->5->5->5->NULL
1->2->3->4->5->NULL
```

Student's Section
Subscribe