How to Remove Duplicate Elements From Java LinkedList? Last Updated : 14 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Example: Initial composition : 7 2 3 3 2 7 6 2 After removing duplicates : 7 2 3 6Pictorial Representation : ( a node in a LinkedList has two parts : data and link to next node (null in case of the last element) Algorithm : Initially, a new node is created which points to the head.A temp node will point to current and index node will point to current.next.If the data of the index node and the current node is same i.e if a duplicate element is found, temp.next is made to point to index.next i.e it skips the duplicate element.If the above condition is not satisfied, then the temp is made to point to the previous node of an index.Index node iterates until the end and steps 3 and 4 are repeated.Steps 2 to 5 are executed till the current node points to the end i.e reaches its end.Below is the implementation of the above approach: Java // Java Program to Remove Duplicate Elements From LinkedList import java.io.*; // Creating the node class for a singly linkedlist class Node { Node next; int data; public Node(int data) { this.data = data; this.next = null; } } public class singlyLinkedList { // Defining the head and tail of a singly linkedlist public Node head = null; public Node tail = null; // creating add() that enables addition // of a new node to the list public void add(int data) { // Creating a new node Node newNode = new Node(data); // Checking whether the list is empty or not if (head == null) { // If the list is found to be empty, both head // and tail are made to point to the new node head = newNode; tail = newNode; } else { // newNode is added after tail in such a way // that next node of the tail points to newNode tail.next = newNode; // newNode becomes the new tail of the list tail = newNode; } } // Creating removeDuplicates() to remove // duplicates from the linkedlist public void removeDuplicates() { // current node points to the head element Node current = head, index = null, temp = null; if (head == null) { return; } else { while (current != null) { // temp node points to the previous node temp = current; // index node points to node next to current index = current.next; while (index != null) { // checking if node of current data is // equal to index node data if (current.data == index.data) { // duplicate node is skipped temp.next = index.next; } else { // temp node points to the previous // node of index node temp = index; } index = index.next; } current = current.next; } } } // creating print() to print all the data // of nodes present in the list public void print() { // Node current will point to head Node current = head; if (head == null) { System.out.println( "Empty list please insert some elements first"); return; } while (current != null) { System.out.print(current.data + " "); // incrementing pointer current = current.next; } System.out.println(); } public static void main(String[] args) { singlyLinkedList List = new singlyLinkedList(); // Adding data to the list List.add(9); List.add(1); List.add(1); List.add(3); List.add(4); List.add(8); List.add(2); List.add(1); System.out.println("Initial composition : "); List.print(); // removing duplicate nodes List.removeDuplicates(); System.out.println("After removing duplicates : "); List.print(); } } OutputInitial composition : 9 1 1 3 4 8 2 1 After removing duplicates : 9 1 3 4 8 2 Time Complexity: O(N2) Comment More infoAdvertise with us Next Article How to Remove Duplicate Elements From Java LinkedList? D dikshapatro Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 java-LinkedList +1 More Practice Tags : Java Similar Reads How to Remove Elements from a LinkedHashMap in Java? A LinkedHashMap is a part of the Collection Framework from java.util package Java and is similar to a HashMap, except that a LinkedHashMap preserves the insertion order among the keys/entries. In this article, we will look at how to remove the elements from a LinkedHashMap in Java. Program to Remove 2 min read How to Remove Duplicate Elements from the Vector in Java? Using LinkedHashSet and TreeSet, duplicate elements are removed. Because the LinkedHashSet and TreeSet do not accept duplicate elements. Example: Input : vector = [1, 2, 3, 4, 2, 4] Output: vector = [1, 2, 3, 4] Input : vector = [a, b, a, c, d, a] Output: vector = [a, b, c, d]Approach 1: Using Linke 3 min read How to find duplicate elements in a Stream in Java Given a stream containing some elements, the task is to find the duplicate elements in this stream in Java. Examples: Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34} Output: [59, 13] Explanation: The only duplicate elements in the given stream are 59 and 13. Input: Stream = {5, 13, 4, 21, 27, 5 min read Remove First and Last Elements from LinkedList in Java The Java.util.LinkedList.removeFirst() method is used to remove the first element from the LinkedList. The Java.util.LinkedList.removeLast() method is used to remove the last element from the LinkedList. Both the methods also returns the element after removing it. 1. removeFirst() Syntax: LinkedList 2 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 Like