# Remove Duplicate Nodes from Unsorted Linked List

Remove Duplicate Nodes from Unsorted Linked List” is one of the foremost problem asked in technical interviews. Here, we are given an unsorted linked list and our task is to remove all duplicates nodes from a given linked list.

To do so, we will use two loops, outer loop to track every element and inner node to traverse the linked and remove every duplicate node.

For example, we need to remove duplicates from the following unsorted list.
LIST:  45 -> 45 -> 46 -> 56 -> 45 -> 68 -> 46
After removing all the duplicates from an unsorted linked list, the list would become:
LIST: 45 -> 46 -> 56 -> 68

The steps required to remove duplicate nodes from an unsorted linked list is as follows:

2. Traverse the list using temp node.
3. Inside traversing loop, use another traversing loop and compare each node element with ‘temp’ node, if the node value matches, then delete that node.

C++ Program to remove duplicate nodes from Unsorted Linked List is as follows:

/* C++ Program to remove duplicate nodes from an unsorted linked list */
#include<bits/stdc++.h>
using namespace std;

/* Structure of the node of the linked list */
typedef struct node
{
int data;
struct node *next;
}Node;

/* Function to Insert node at the end of the linked list */
{
/* Creating a new node */
Node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;

/* check whether the linked list is empty or not */

/* If Linked List is empty, then new node will become head of the linked list */
{
return;
}
/* If Linked List is not empty, then traverse the complete linked list and point the pointer
of last node of the linked list to the new node */
Node *temp = NULL;
while(temp->next != NULL)
temp=temp->next;
temp->next = new_node;
}

/* Function to remove duplicate Nodes from the linked list */
{
Node *temp = NULL;
/* Outer Loop is used to track current node */
while(curr != NULL)
{
temp = curr;
/* Innser loop is used to delete multiple occurrences of the current node */
while(temp->next != NULL)
{
if(temp->next->data == curr->data)
{
Node *p = temp->next;
temp->next = temp->next->next;
delete p;
}
else
temp = temp->next;
}
curr = curr->next;
}
}

/* Function to Print the Linked List  */
{
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main()
{
/* Inserting Some Nodes in the Linked List */
cout<<"Linked List Before Removing Duplicate Nodes:\n";