C Program For Removing Duplicates From A Sorted Linked List
Last Updated :
03 Apr, 2023
Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once.
For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60.
Algorithm:
Traverse the list from the head (or start) node. While traversing, compare each node with its next node. If the data of the next node is the same as the current node then delete the next node. Before we delete a node, we need to store the next pointer of the node
Implementation:
Functions other than removeDuplicates() are just to create a linked list and test removeDuplicates().
C
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void removeDuplicates( struct Node* head)
{
struct Node* current = head;
struct Node* next_next;
if (current == NULL)
return ;
while (current->next != NULL)
{
if (current->data == current->next->data)
{
next_next = current->next->next;
free (current->next);
current->next = next_next;
}
else
{
current = current->next;
}
}
}
void push( struct Node** head_ref,
int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node *node)
{
while (node!=NULL)
{
printf ( "%d " , node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
printf (
"Linked list before duplicate removal " );
printList(head);
removeDuplicates(head);
printf (
"Linked list after duplicate removal " );
printList(head);
return 0;
}
|
Output:
Linked list before duplicate removal 11 11 11 13 13 20
Linked list after duplicate removal 11 13 20
Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(1)
Recursive Approach :
C
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void push( struct Node** head_ref,
int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node *node)
{
while (node!=NULL)
{
printf ( "%d " , node->data);
node = node->next;
}
}
Node* deleteDuplicates(Node* head)
{
if (head == nullptr)
return nullptr;
if (head->next == nullptr)
return head;
if (head->data == head->next->data)
{
Node *tmp;
tmp = head->next;
head->next = head->next->next;
free (tmp);
return deleteDuplicates(head);
}
else
{
head->next = deleteDuplicates(head->next);
return head;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
printf (
"Linked list before duplicate removal " );
printList(head);
head = deleteDuplicates(head);
printf (
"Linked list after duplicate removal " );
printList(head);
return 0;
}
|
Output:
Linked list before duplicate removal 11 11 11 13 13 20
Linked list after duplicate removal 11 13 20
Time Complexity: O(n), where n is the number of nodes in the given linked list.
Auxiliary Space: O(n), due to recursive stack where n is the number of nodes in the given linked list.
Please refer complete article on Remove duplicates from a sorted linked list for more details!
Share your thoughts in the comments
Please Login to comment...