Write a removeDuplicates() function which takes a list and deletes any duplicate nodes from the list. The list is not sorted.

For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43.

METHOD 1 (Using two loops)
This is the simple way where two loops are used. Outer loop is used to pick the elements one by one and inner loop compares the picked element with rest of the elements.

Java Programming:

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20remove%20duplicates%20from%20unsorted%20%0A%2F%2F%20linked%20list%0A%20%0Aclass%20LinkedList%20%7B%0A%20%0A%20%20%20%20static%20Node%20head%3B%0A%20%0A%20%20%20%20static%20class%20Node%20%7B%0A%20%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%0A%20%20%20%20%20%20%20%20Node(int%20d)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Function%20to%20remove%20duplicates%20from%20an%0A%20%20%20%20%20%20%20unsorted%20linked%20list%20*%2F%0A%20%20%20%20void%20remove_duplicates()%20%7B%0A%20%20%20%20%20%20%20%20Node%20ptr1%20%3D%20null%2C%20ptr2%20%3D%20null%2C%20dup%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20ptr1%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Pick%20elements%20one%20by%20one%20*%2F%0A%20%20%20%20%20%20%20%20while%20(ptr1%20!%3D%20null%20%26%26%20ptr1.next%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20ptr2%20%3D%20ptr1%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Compare%20the%20picked%20element%20with%20rest%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20of%20the%20elements%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(ptr2.next%20!%3D%20null)%20%7B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20If%20duplicate%20then%20delete%20it%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(ptr1.data%20%3D%3D%20ptr2.next.data)%20%7B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20sequence%20of%20steps%20is%20important%20here%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dup%20%3D%20ptr2.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ptr2.next%20%3D%20ptr2.next.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.gc()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20%2F*%20This%20is%20tricky%20*%2F%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ptr2%20%3D%20ptr2.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20ptr1%20%3D%20ptr1.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20void%20printList(Node%20node)%20%7B%0A%20%20%20%20%20%20%20%20while%20(node%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(node.data%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20node%20%3D%20node.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20list%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20list.head%20%3D%20new%20Node(10)%3B%0A%20%20%20%20%20%20%20%20list.head.next%20%3D%20new%20Node(12)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next%20%3D%20new%20Node(11)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next%20%3D%20new%20Node(11)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next%20%3D%20new%20Node(12)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next.next%20%3D%20new%20Node(11)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next.next.next%20%3D%20new%20Node(10)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20List%20before%20removing%20duplicates%20%3A%20%5Cn%20%22)%3B%0A%20%20%20%20%20%20%20%20list.printList(head)%3B%0A%20%0A%20%20%20%20%20%20%20%20list.remove_duplicates()%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20List%20after%20removing%20duplicates%20%3A%20%5Cn%20%22)%3B%0A%20%20%20%20%20%20%20%20list.printList(head)%3B%0A%20%20%20%20%7D%0A%7D%0A%2F%2F%20This%20code%20has%20been%20contributed%20by%20Mayank%20Jaiswal” message=”” highlight=”” provider=”manual”/]

Output :

Linked list before removing duplicates:
 10 12 11 11 12 11 10 
Linked list after removing duplicates:
 10 12 11

Time Complexity: O(n^2)

METHOD 2 (Use Sorting)
In general, Merge Sort is the best suited sorting algorithm for sorting linked lists efficiently.
1) Sort the elements using Merge Sort. We will soon be writing a post about sorting a linked list. O(nLogn)
2) Remove duplicates in linear time using the algorithm for removing duplicates in sorted Linked List. O(n)

Please note that this method doesn’t preserve the original order of elements.

Time Complexity: O(nLogn)

METHOD 3 (Use Hashing)
We traverse the link list from head to end. For every newly encountered element, we check whether it is in the hash table: if yes, we remove it; otherwise we put it in the hash table.

C++ Programming:

[pastacode lang=”cpp” manual=”%2F*%20Program%20to%20remove%20duplicates%20in%20an%20unsorted%0A%20%20%20linked%20list%20*%2F%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F*%20A%20linked%20list%20node%20*%2F%0Astruct%20Node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20Node%20*next%3B%0A%7D%3B%0A%20%0A%2F%2F%20Utility%20function%20to%20create%20a%20new%20Node%0Astruct%20Node%20*newNode(int%20data)%0A%7B%0A%20%20%20Node%20*temp%20%3D%20new%20Node%3B%0A%20%20%20temp-%3Edata%20%3D%20data%3B%0A%20%20%20temp-%3Enext%20%3D%20NULL%3B%0A%20%20%20return%20temp%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20remove%20duplicates%20from%20a%0A%20%20%20unsorted%20linked%20list%20*%2F%0Avoid%20removeDuplicates(struct%20Node%20*start)%0A%7B%0A%20%20%20%20%2F%2F%20Hash%20to%20store%20seen%20values%0A%20%20%20%20unordered_set%3Cint%3E%20seen%3B%0A%20%0A%20%20%20%20%2F*%20Pick%20elements%20one%20by%20one%20*%2F%0A%20%20%20%20struct%20Node%20*curr%20%3D%20start%3B%0A%20%20%20%20struct%20Node%20*prev%20%3D%20NULL%3B%0A%20%20%20%20while%20(curr%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20current%20value%20is%20seen%20before%0A%20%20%20%20%20%20%20%20if%20(seen.find(curr-%3Edata)%20!%3D%20seen.end())%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20prev-%3Enext%20%3D%20curr-%3Enext%3B%0A%20%20%20%20%20%20%20%20%20%20%20delete%20(curr)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20seen.insert(curr-%3Edata)%3B%0A%20%20%20%20%20%20%20%20%20%20%20prev%20%3D%20curr%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20curr%20%3D%20prev-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20linked%20list%20*%2F%0Avoid%20printList(struct%20Node%20*node)%0A%7B%0A%20%20%20%20while%20(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20node-%3Edata)%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20The%20constructed%20linked%20list%20is%3A%0A%20%20%20%20%2010-%3E12-%3E11-%3E11-%3E12-%3E11-%3E10*%2F%0A%20%20%20%20struct%20Node%20*start%20%3D%20newNode(10)%3B%0A%20%20%20%20start-%3Enext%20%3D%20newNode(12)%3B%0A%20%20%20%20start-%3Enext-%3Enext%20%3D%20newNode(11)%3B%0A%20%20%20%20start-%3Enext-%3Enext-%3Enext%20%3D%20newNode(11)%3B%0A%20%20%20%20start-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%20newNode(12)%3B%0A%20%20%20%20start-%3Enext-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20newNode(11)%3B%0A%20%20%20%20start-%3Enext-%3Enext-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20newNode(10)%3B%0A%20%0A%20%20%20%20printf(%22Linked%20list%20before%20removing%20duplicates%20%3A%20%5Cn%22)%3B%0A%20%20%20%20printList(start)%3B%0A%20%0A%20%20%20%20removeDuplicates(start)%3B%0A%20%0A%20%20%20%20printf(%22%5CnLinked%20list%20after%20removing%20duplicates%20%3A%20%5Cn%22)%3B%0A%20%20%20%20printList(start)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output :

Linked 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).