Java Program For Removing Duplicates From An Unsorted Linked List
Last Updated :
28 Jun, 2023
Given an unsorted Linked List, the task is to remove duplicates from the list.
Examples:
Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21
Output: 12 -> 11 -> 21 -> 41 -> 43
Explanation: Second occurrence of 12 and 21 are removed.
Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21
Output: 12 -> 11 -> 21 -> 41 -> 43
Naive Approach to Remove Duplicates from an Unsorted Linked List:
The most simple approach to solve this, is to check each node for duplicate in the Linked List one by one.
Below is the Implementation of the above approach:
Java
// Java program to remove duplicates from unsorted
// linked list
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Function to remove duplicates from an
unsorted linked list */
void remove_duplicates()
{
Node ptr1 = null, ptr2 = null, dup = null;
ptr1 = head;
/* Pick elements one by one */
while (ptr1 != null && ptr1.next != null) {
ptr2 = ptr1;
/* Compare the picked element with rest
of the elements */
while (ptr2.next != null) {
/* If duplicate then delete it */
if (ptr1.data == ptr2.next.data) {
/* sequence of steps is important here
*/
ptr2.next = ptr2.next.next;
System.gc();
}
else /* This is tricky */ {
ptr2 = ptr2.next;
}
}
ptr1 = ptr1.next;
}
}
void printList(Node node)
{
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(10);
list.head.next = new Node(12);
list.head.next.next = new Node(11);
list.head.next.next.next = new Node(11);
list.head.next.next.next.next = new Node(12);
list.head.next.next.next.next.next = new Node(11);
list.head.next.next.next.next.next.next
= new Node(10);
System.out.println(
"Linked List before removing duplicates : ");
list.printList(head);
list.remove_duplicates();
System.out.println("\n");
System.out.println(
"Linked List after removing duplicates : ");
list.printList(head);
}
}
// This code has been contributed by Mayank Jaiswal
OutputLinked List before removing duplicates :
10 12 11 11 12 11 10
Linked List after removing duplicates :
10 12 11
Time Complexity: O(N2)
Auxiliary Space: O(1)
Remove duplicates from an Unsorted Linked List using Sorting:
Follow the below steps to Implement the idea:
Below is the implementation for above approach:
Java
import java.io.*;
// structure of a node in the linked list
class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
// class for the linked list
class LinkedList {
Node head;
// function to insert a node in the linked list
public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
// function to sort the linked list
public void sortList()
{
// pointer to traverse the linked list
Node current = head;
Node index = null;
// loop to traverse the linked list
while (current != null) {
// loop to compare current node with all other
// nodes
index = current.next;
while (index != null) {
// checking for duplicate values
if (current.data == index.data) {
// deleting the duplicate node
current.next = index.next;
index = current.next;
}
else {
index = index.next;
}
}
current = current.next;
}
}
// function to display the linked list
public void printList()
{
Node node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
}
// main class
class Main {
public static void main(String[] args)
{
LinkedList ll = new LinkedList();
ll.push(20);
ll.push(13);
ll.push(13);
ll.push(11);
ll.push(11);
ll.push(11);
System.out.println(
"Linked List before removing duplicates : ");
ll.printList();
ll.sortList();
System.out.println(
"Linked List after removing duplicates : ");
ll.printList();
}
}
OutputLinked List before removing duplicates :
11 11 11 13 13 20
Linked List after removing duplicates :
11 13 20
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Remove duplicates from an Unsorted Linked List using Hashing:
The idea for this approach is based on the following observations:
- Traverse the link list from head to end.
- For every newly encountered element, check whether if it is in the hash table:
- if yes, remove it
- otherwise put it in the hash table.
- At the end, the Hash table will contain only the unique elements.
Below is the implementation of the above approach:
Java
// Java program to remove duplicates
// from unsorted linkedlist
import java.util.HashSet;
public class removeDuplicates {
static class node {
int val;
node next;
public node(int val) { this.val = val; }
}
/* Function to remove duplicates from a
unsorted linked list */
static void removeDuplicate(node head)
{
// Hash to store seen values
HashSet<Integer> hs = new HashSet<>();
/* Pick elements one by one */
node current = head;
node prev = null;
while (current != null) {
int curval = current.val;
// If current value is seen before
if (hs.contains(curval)) {
prev.next = current.next;
}
else {
hs.add(curval);
prev = current;
}
current = current.next;
}
}
/* Function to print nodes in a given linked list */
static void printList(node head)
{
while (head != null) {
System.out.print(head.val + " ");
head = head.next;
}
}
public static void main(String[] args)
{
/* The constructed linked list is:
10->12->11->11->12->11->10*/
node start = new node(10);
start.next = new node(12);
start.next.next = new node(11);
start.next.next.next = new node(11);
start.next.next.next.next = new node(12);
start.next.next.next.next.next = new node(11);
start.next.next.next.next.next.next = new node(10);
System.out.println(
"Linked list before removing duplicates :");
printList(start);
removeDuplicate(start);
System.out.println(
"\nLinked list after removing duplicates :");
printList(start);
}
}
// This code is contributed by Rishabh Mahrsee
OutputLinked list before removing duplicates :
10 12 11 11 12 11 10
Linked list after removing duplicates :
10 12 11
Time Complexity: O(N), on average (assuming that hash table access time is O(1) on average).
Auxiliary Space: O(N), As extra space is used to store the elements in the stack.
Similar Reads
Java 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
Java Program For Removing All Occurrences Of Duplicates From A Sorted Linked List Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. Examples: Input: 23->28->28->35->49->49->53->53 Output: 23->35 Input: 11->11->11->11->75->75 Output: empty List Note that this is different from Rem
3 min read
Java Program For Insertion Sort In A Singly Linked List We have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list. Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list. 2) Traverse the given list, do following for every node. ......a) Insert curr
4 min read
Java 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
Java Program For Writing A Function To Delete A Linked List Algorithm For Java:In Java, automatic garbage collection happens, so deleting a linked list is easy. Just need to change head to null. Implementation: Java // Java program to delete a linked list class LinkedList { // Head of the list Node head; // Linked List node class Node { int data; Node next;
2 min read
Java Program For Removing Every K-th Node Of The Linked List Given a singly linked list, Your task is to remove every K-th node of the linked list. Assume that K is always less than or equal to length of Linked List.Examples : Input: 1->2->3->4->5->6->7->8 k = 3 Output: 1->2->4->5->7->8 As 3 is the k-th node after its deletion list would be 1->2->4->5->6->7->
3 min read
How to Remove Duplicate Elements From Java LinkedList? Linked List is a part of the Collection in java.util package. LinkedList class is an implementation of the LinkedList data structure it is a linear data structure. In LinkedList due to the dynamical allocation of memory, insertions and deletions are easy processes. For removing duplicates from Examp
4 min read
Java Program For Union And Intersection Of Two Linked Lists Write a java program for a given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.Example: Input:List1: 10->15->4->20List2: 8->4->2->10Output:In
10 min read
Java 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-
5 min read
Java Program to Remove Duplicate Elements From the Array Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted.Example:Java// Java Program to Remove Duplicate // Ele
6 min read