# Remove duplicates from a sorted linked list

Write a removeDuplicates() function which 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.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Algorithm:
Traverse the list from the head (or start) node. While traversing, compare each node with its next node. If data of next node is same as current node then delete the next node. Before we delete a node, we need to store next pointer of the node

Implementation:
Functions other than removeDuplicates() are just to create a linked linked list and test removeDuplicates().

## C

```/* C Program to remove duplicates from a sorted linked list */
#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node* next;
};

/* The function removes duplicates from a sorted list */
{
/* Pointer to traverse the linked list */

/* 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;
}
else /* This is tricky: only advance if no deletion */
{
current = current->next;
}
}
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginging 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 off the new node */

/* move the head to point to the 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;
}
}

/* Drier program to test above functions*/
int main()
{

/* Let us create a sorted linked list to test the functions
Created linked list will be 11->11->11->13->13->20 */

printf("\n Linked list before duplicate removal  ");

/* Remove duplicates from linked list */

printf("\n Linked list after duplicate removal ");

return 0;
}
```

## Java

```// Java program to remove duplicates from a sorted linked list
{

class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}

void removeDuplicates()
{

/* Pointer to store the next pointer of a node to be deleted*/
Node next_next;

/* do nothing if the list is empty */
return;

/* Traverse list till the last node */
while (current.next != null) {

/*Compare current node with the next node */
if (current.data == current.next.data) {
next_next = current.next.next;
current.next = null;
current.next = next_next;
}
else // advance if no deletion
current = current.next;
}
}

/* Utility functions */

/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);

/* 3. Make next of new Node as head */

/* 4. Move the head to point to new Node */
}

/* Function to print linked list */
void printList()
{
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}

/* Drier program to test above functions */
public static void main(String args[])
{
llist.push(20);
llist.push(13);
llist.push(13);
llist.push(11);
llist.push(11);
llist.push(11);

System.out.println("List before removal of duplicates");
llist.printList();

llist.removeDuplicates();

System.out.println("List after removal of elements");
llist.printList();
}
}
/* This code is contributed by Rajat Mishra */
```

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 number of nodes in the given linked list.

Related Article :
Remove all occurrences of duplicates from a sorted Linked List

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.6 Average Difficulty : 1.6/5.0
Based on 172 vote(s)