Explore
Placement Prep

Edit

Edit

# Remove duplicates from a linked list (sorted and unsorted linked list) | FACE Prep

Published on 11 Mar 2020

Program to remove duplicates from a linked list (sorted and unsorted linked list) is discussed here. Given a linked list, print the linked list remove duplicate elements from it.

### Remove duplicates from a sorted linked list

Input: 1 -> 2 -> 2 -> 3 -> 4 -> 5 -> 5

Output: 1 -> 2 -> 3 -> 4 -> 5

### Remove duplicates from an unsorted linked list

Input: 7 -> 7 -> 19 -> 11 -> 9 -> 11

Output: 7 -> 19 -> 11 -> 9

## Algorithm to remove duplicates from a linked list (sorted)

• Input the number of elements of the linked list.
• Input the elements of the linked list in sorted order.
• While traversing, compare the current node with the next node.
• If data of the next node is the same as the current node then delete the next node.

### Program to remove duplicates from a sorted linked list

C
C++

Output
Input - Enter the total number of elements : 10 Enter the sorted linked list : 1 2 3 3 4 4 5 5 5 6 Linked list before removing duplicates : 6 5 5 5 4 4 3 3 2 1 Output - Linked list after removing duplicates : 6 5 4 3 2 1

## Algorithm to remove duplicates from a linked list (unsorted)

• Input the number of elements of the linked list.
• Input the elements of the linked list.
• Use two loops, one for traversing the linked list and the other loop to check if the current element is already present in the list.

### Program to remove duplicate elements from an unsorted linked list

C
C++

Output
Input - Enter the total number of elements : 6 Enter the sorted linked list : 7 7 19 11 9 11 Linked list before removing duplicates : 11 9 11 19 7 7 Output - Linked list after removing duplicates : 11 9 19 7

Recommended Programs