  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Remove duplicates from a sorted linked list ### Introduction

We know that a linked list is a mutable data structure. And it might have duplicate elements in it. We basically have two types of linked lists: one is a sorted linked list and another is an unsorted linked list. Here we will see an approach to remove duplicates from sorted list

### Problem Statement on how to remove duplicates from sorted linked list

In this problem, we are given a sorted LinkedList and are asked to remove the duplicate elements present in the list.

### Problem Statement Understanding on how to remove duplicates from sorted list.

According to the problem statement, we will be given a sorted linked list that may contain duplicate elements. We need to remove the duplicate elements from the list.

Let’s try to understand the problem with the help of some examples.

If the given input sorted linked list is: • As we can see in the above list, there are multiple occurrences of elements 10, 12 in the linked list.
• So it means that the list contains duplicates of 10, 12 , and we need to remove these duplicates from the linked list.
• After removing the duplicate elements from the list, the output linked list will be: • In this case, as we can see in the above list, 11 and 40 are duplicate elements present more than once in the list.
• After removing the duplicate elements from the list, the output will be head->11->25->40->45.
##### Some more examples:

Now, I think from the above examples, the problem statement is clear. So let’s see how we will approach it.

Before moving to the approach section, try to think about how you can approach this problem.

• If stuck, no problem, we will thoroughly see how we can approach the problem in the next section.

Let’s move to the approach section.

### Approach 1 (Using Hashmaps) on how to remove duplicates from sorted list

Our approach will be simple; we will be using hashmap:

• While traversing the Linked List, for every element, we will check if it is already present in the map or not.
• If it is already present, then we will remove the element from the list.
• Else we will push the element in the hash map and move forward.

This approach will require extra space.

Time complexity: O(n), where n is the number of nodes in the linked list.

Space complexity: O(n), where n is the number of nodes in the linked list.

Although the above approach will work fine, but it requires some extra space. Now the main question is do we necessarily need this extra space? Can we remove duplicates from the sorted linked list without consuming extra space?

• Let’s see in the next section how we can solve this problem without using extra space.

Let’s move to the next approach.

### Approach 2 (Using two pointers)on how to remove duplicates from sorted linked list

Our approach will be straightforward:

• We will create a pointer prev that will to the first occurrence of the element and second pointer temp to traverse the linked list.
• If the value of the temp pointer is equal to the prev pointer, temp will move forward and when we find the node whose value is not equal to prev pointer, we will connect the node pointer by prev with the node pointed by temp and move prev pointer to the position of temp and move temp forward.

From the above approach, we can see that we are using constant extra space here.

Time complexity: O(n), where n is the number of nodes in the linked list.

Space complexity: O(1), As constant extra space, is required.

Let’s see another approach that solves this problem in similar time and space complexity using two pointers for the sake of learning.

### Approach 3

The idea is very simple:

• Traverse the list using a pointer, say current.
• While traversing the list compare the current node’s value with the value of it’s next node.
• If they are the same, remove the next node.
• Else move forward.
• Keep on doing this till we reach the end of the linked list.

### Algorithm on how to remove duplicates from sorted list

• Create a pointer current.
• Traverse the Linked List in a linear fashion.
• For every node, check the next node’s data.
• If they are the same, then delete the current’s next node by changing the current node pointer.
• If they are different, then move current pointer forward (current = current->next).
• Keep on doing this until current->next != NULL or we can say until we reach the end of the linked list.

### Dry Run on how to remove duplicates from sorted linked list  ### Code Implementation on how to remove duplicates from sorted 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 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 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;
}
}

/* Driver 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;
}
```
```#include <bits/stdc++.h>
using namespace std;

/* Node structure of the linked list nodes */
class Node{
public:
int data;
Node* next;
};

/* Using this function we will be removing the duplicates elements from the given sorted linked list */
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;
}
}
}

/* Using this function we will be pushing a new node at the head of the linked list */
Node* new_node = new Node();
new_node->data = data;
}

/* Using this function we will be printing the linked list */
while (node!=NULL){
cout<<" "<<node->data;
node = node->next;
}
cout<<endl;
}

int main()
{

return 0;
}

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

void removeDuplicates()
{

/* Traverse list till the last node */
while (curr != null) {
Node temp = curr;
/*Compare current node with the next node and
keep on deleting them until it matches the current
node data */
while(temp!=null && temp.data==curr.data) {
temp = temp.next;
}
/*Set current node next to the next different
element denoted by temp*/
curr.next = temp;
curr = curr.next;
}
}
void push(int new_data)
{
Node new_node = new Node(new_data);
}
void printList()
{
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
public static void main(String args[])
{
DeleteDuplicates llist = new DeleteDuplicates();
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();
}
}
```
```class Node:

def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

def push(self, new_data):
new_node = Node(new_data)

def deleteNode(self, key):

if (temp is not None):
if (temp.data == key):
temp = None
return

while(temp is not None):
if temp.data == key:
break
prev = temp
temp = temp.next

if(temp == None):
return

prev.next = temp.next
temp = None

def printList(self):
while(temp):
print(temp.data , end = ' ')
temp = temp.next

def removeDuplicates(self):
if temp is None:
return
while temp.next is not None:
if temp.data == temp.next.data:
new = temp.next.next
temp.next = None
temp.next = new
else:
temp = temp.next

llist.push(35)
llist.push(30)
llist.push(12)
llist.push(10)
llist.push(10)
llist.push(10)
llist.printList()
print()
"duplicate elements:")
llist.removeDuplicates()
llist.printList()
```

#### Output

10 10 10 12 30 35

10 12 30 35

Time Complexity: O(n), single traversal of the list require O(n) time, where n is the number of nodes in the linked list.

### Conclusion

In this blog, we have seen an approach on how to remove duplicates from sorted list in linear time and haven’t used extra space too. We have also seen the traversing through the linked list in linear fashion.we have understood different types of approaches, dry run and code implementations on how to remove duplicates from sorted linked list. This is a basic problem and is good for strengthening your concepts in LinkedList and if you want to practice more such problems, you can checkout Prepbytes (Linked List).

## FAQs on how to remove duplicates from sorted list.

1. What are the approaches which are used to remove duplicates from sorted list?
2. There are two known approaches :

• Approach -1 using Hashmaps.
• Approach -2 using pointers.
3. What is the time complexity to remove duplicates from sorted list?
4. The time complexity to remove duplicates from sorted list is O(n).

5. How is the data stored in the linked list?
6. Each element in the linked list is stored in the form of a node where node is a collection of two parts in which one part is a data part which has a value and the next part is a pointer which stores the address of the next node.