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
// C Program to remove duplicates
// from a sorted linked list
#include<stdio.h>
#include<stdlib.h>
// Link list node
struct Node
{
int data;
struct Node* next;
};
// The function removes duplicates
// from a sorted list
void removeDuplicates(struct Node* head)
{
// Pointer to traverse the linked list
struct Node* current = head;
// Pointer to store the next pointer
// of a node to be deleted
struct Node* next_next;
// Do nothing if the list is empty
if (current == NULL)
return;
// Traverse the list till last node
while (current->next != NULL)
{
// Compare current node with next node
if (current->data == current->next->data)
{
// The sequence of steps is important
next_next = current->next->next;
free(current->next);
current->next = next_next;
}
// This is tricky: only advance
// if no deletion
else
{
current = current->next;
}
}
}
// UTILITY FUNCTIONS
// Function to insert a node at the
// beginning of the linked list
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 of 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;
}
}
// Driver code
int main()
{
// Start with the empty list
struct Node* head = NULL;
/* Let us create a sorted linked list
to test the functions. Created
linked list will be
11->11->11->13->13->20 */
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);
// Remove duplicates from linked list
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
// C recursive Program to remove duplicates
// from a sorted linked list
#include<stdio.h>
#include<stdlib.h>
// Link list node
struct Node
{
int data;
struct Node* next;
};
// UTILITY FUNCTIONS
// Function to insert a node at
// the beginning of the linked list
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 of 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;
}
}
Node* deleteDuplicates(Node* head)
{
if (head == nullptr)
return nullptr;
if (head->next == nullptr)
return head;
if (head->data == head->next->data)
{
Node *tmp;
// If find next element duplicate,
// preserve the next pointer to be
// deleted, skip it, and then delete
// the stored one. Return head
tmp = head->next;
head->next = head->next->next;
free(tmp);
return deleteDuplicates(head);
}
else
{
// if doesn't find next element duplicate, leave head
// and check from next element
head->next = deleteDuplicates(head->next);
return head;
}
}
// Driver code
int main()
{
// Start with the empty list
struct Node* head = NULL;
/* Let us create a sorted linked list
to test the functions. Created
linked list will be 11->11->11->13->13->20 */
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);
// Remove duplicates from linked list
head = deleteDuplicates(head);
printf(
"Linked list after duplicate removal ");
printList(head);
return 0;
}
// This code is contributed by Yogesh shukla
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!
Similar Reads
C# Program For Removing Duplicates From A Sorted Linked List 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-
8 min read
C# Program For Removing Duplicates From An Unsorted Linked List Write a removeDuplicates() function that 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. Recommended:
4 min read
C Program to Remove Duplicates from Sorted Array In this article, we will learn how to remove duplicates from a sorted array using the C program.The most straightforward method is to use the two-pointer approach which uses two pointers: one pointer to iterate over the array and other to track duplicate elements. In sorted arrays, duplicates are ad
4 min read
C Program For Writing A Function To Delete A Linked List Algorithm For C:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C // C program to delete a linked list #include<stdio.h> #include<stdlib.h> #includ
2 min read
C Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l
3 min read
C Program For Searching An Element In A Linked List Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functi
4 min read
C Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2-
9 min read
C# Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2->4->6->8, Ou
4 min read
C Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15-
4 min read