Remove duplicates from an unsorted linked list
Write a removeDuplicates() function which takes a list and deletes any duplicate nodes from the list. The list is not sorted.
For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43.
METHOD 1 (Using two loops)
This is the simple way where two loops are used. Outer loop is used to pick the elements one by one and inner loop compares the picked element with rest of the elements.
Thanks to Gaurav Saxena for his help in writing this code.
/* Program to remove duplicates in an unsorted array */
#include<stdio.h>
#include<stdlib.h>
/* A linked list node */
struct node
{
int data;
struct node *next;
};
/* Function to remove duplicates from a unsorted linked list */
void removeDuplicates(struct node *start)
{
struct node *ptr1, *ptr2, *dup;
ptr1 = start;
/* Pick elements one by one */
while(ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* Compare the picked element with rest of the elements */
while(ptr2->next != NULL)
{
/* If duplicate then delete it */
if(ptr1->data == ptr2->next->data)
{
/* sequence of steps is important here */
dup = ptr2->next;
ptr2->next = ptr2->next->next;
free(dup);
}
else /* This is tricky */
{
ptr2 = ptr2->next;
}
}
ptr1 = ptr1->next;
}
}
/* UTILITY FUNCTIONS */
/* Function to push a node */
void push(struct node** head_ref, int new_data);
/* Function to print nodes in a given linked list */
void printList(struct node *node);
/* Druver program to test above function */
int main()
{
struct node *start = NULL;
/* The constructed linked list is:
10->12->11->11->12->11->10*/
push(&start, 10);
push(&start, 11);
push(&start, 12);
push(&start, 11);
push(&start, 11);
push(&start, 12);
push(&start, 10);
printf("\n Linked list before removing duplicates ");
printList(start);
removeDuplicates(start);
printf("\n Linked list after removing duplicates ");
printList(start);
getchar();
}
/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print nodes in a given linked list */
void printList(struct node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
Time Complexity: O(n^2)
METHOD 2 (Use Sorting)
In general, Merge Sort is the best suited sorting algorithm for sorting linked lists efficiently.
1) Sort the elements using Merge Sort. We will soon be writing a post about sorting a linked list. O(nLogn)
2) Remove duplicates in linear time using the algorithm for removing duplicates in sorted Linked List. O(n)
Time Complexity: O(nLogn)
METHOD 3 (Use Hashing)
We traverse the link list from head to end. For the newly encountered element, we check whether it is in the hash table: if yes, we remove it; otherwise we put it in the hash table.
Thanks to bearwang for suggesting this method.
Time Complexity: O(n) on average (assuming that hash table access time is O(1) on average).
Please write comments if you find any of the above explanations/algorithms incorrect, or a better ways to solve the same problem.


where is code for deleting using hash table ??
really nice answers… thank you geeks4geeks
Java Code:
public class ListNode { int data; ListNode next; } public removeDuplicatesUnSorted( ListNode p ) { while( p != null ) { // Remove all duplicates in list that match p // Need to check all nodes succeeding p since list is unsorted ListNode q = p; // start with p to allow next node delete while( q != null ) { // Is next node to q a duplicate for p? if( q.next != null && q.next.data == p.data ) q.next = q.next.next; // remove successive duplicates else q = q.next; // advance q to next node (non-duplicate) } p = p.next; // advance p } }We have added the Hash Table method to the original post. Thanks all for you contribution,
i need this code for deleting using hash table…
dou you have it??
when it comes to display entries in the hashmap or hashtable after doing whatever manipulation we want, we can’t predict the ordering
By ordering u mean the sequence of elements in the list. How come using a hash table effect that. I don’t understand. Kindly explain by an example.
wat is wrong with your approach is
HashTable or hashmap doesn’t guarantee the ordering. but linked list is ordered data structure right..
Regards
Sathiyan K
How about using an extra hash table?
We traverse the link list from head to end.
for the newly encountered element, we check whether it is in the hash table: if yes, we remove it; otherwise we put it in the hash table.
The running time is O(n)
Space complexity is also O(n).
@bearwang: How does hashing guarantee O(n) worst case time complexity here ?
We sequentially search the link list. Once we reach the end of the link list, the work is done. So it is O(n), assuming that hash can access the element in O(1). Right?
@bearwang: Thanks for the clarification. I am not sure if we can assume that hash can access element in O(1) time.
@kartik: We could use a Bitmap. That would be O(1).