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
Intial composition : 7 2 3 3 2 7 6 2 After removing duplicates : 7 2 3 6
Pictorial Representation : ( a node in a LinkedList has two parts : data and link to next node (null in case of the last element)
- 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:
Intial composition : 9 1 1 3 4 8 2 1 After removing duplicates : 9 1 3 4 8 2
Time Complexity: O(N2)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.