Java program to remove duplicate elements from a singly linked list
In this program, we need to remove the duplicate nodes from the given singly linked list.
List after removing duplicate nodes:
In the above list, node 2 is repeated thrice, and node 1 is repeated twice. Node current will point to head, and index will point to node next to current. Start traversing the list till a duplicate is found that is when current's data is equal to index's data. In the above example, the first duplicate will be found at position 4. Assign current to another node temp. Connect temp's next node with index's next node. Delete index which was pointing to duplicate node. This process will continue until all duplicates are removed.
a. removeDuplicate() will remove duplicate nodes from the list.
a. display() will display the nodes present in the list:
Originals list: 1 2 3 2 2 4 1 List after removing duplicates: 1 2 3 4