How to remove duplicates from a sorted linked list ?

Answer : To write a removeDuplicates() function which takes a list sorted…

How to remove duplicates from a sorted linked list ?

  • To write a removeDuplicates() function which takes a list sorted from the list any duplicate nodes to be deleted. The list should be traversed only once.

Algorithm

  • Traverse the head (or start) node from the list. Compare each node with its next node while traversing.
  • If same as the current node and data of next node to be delete that node. Before we delete a node, we need to store next pointer of the node.

Implementation

  • Functions other than removeDuplicates() are just to create a linked.
  • Linked list to be tested and removeDuplicates() from the list.
 Remove Duplicate From a Sorting Linked List

Sample Code in Java

[pastacode lang=”javascript” manual=”class%20LinkedListExample%20%0A%7B%20%0A%09Node%20head%3B%20%2F%2F%20head%20of%20list%20%0A%0A%09%2F*%20Linked%20list%20Node*%2F%0A%09class%20Node%20%0A%09%7B%20%0A%09%09int%20data%3B%20%0A%09%09Node%20next%3B%20%0A%09%09Node(int%20d)%20%7Bdata%20%3D%20d%3B%20next%20%3D%20null%3B%20%7D%20%0A%09%7D%20%0A%0A%09void%20removeDuplicates()%20%0A%09%7B%20%0A%09%09%2F*Another%20reference%20to%20head*%2F%0A%09%09Node%20current%20%3D%20head%3B%20%0A%0A%09%09%2F*%20Pointer%20to%20store%20the%20next%20pointer%20of%20a%20node%20to%20be%20deleted*%2F%0A%09%09Node%20next_next%3B%20%0A%0A%09%09%2F*%20do%20nothing%20if%20the%20list%20is%20empty%20*%2F%0A%09%09if%20(head%20%3D%3D%20null)%09%20%0A%09%09%09return%3B%20%0A%0A%09%09%2F*%20Traverse%20list%20till%20the%20last%20node%20*%2F%0A%09%09while%20(current.next%20!%3D%20null)%20%7B%20%0A%0A%09%09%09%2F*Compare%20current%20node%20with%20the%20next%20node%20*%2F%0A%09%09%09if%20(current.data%20%3D%3D%20current.next.data)%20%7B%20%0A%09%09%09%09next_next%20%3D%20current.next.next%3B%20%0A%09%09%09%09current.next%20%3D%20null%3B%20%0A%09%09%09%09current.next%20%3D%20next_next%3B%20%0A%09%09%09%7D%20%0A%09%09%09else%20%2F%2F%20advance%20if%20no%20deletion%20%0A%09%09%09current%20%3D%20current.next%3B%20%0A%09%09%7D%20%0A%09%7D%20%0A%09%09%09%09%09%0A%09%2F*%20Utility%20functions%20*%2F%0A%0A%09%2F*%20Inserts%20a%20new%20Node%20at%20front%20of%20the%20list.%20*%2F%0A%09public%20void%20push(int%20new_data)%20%0A%09%7B%20%0A%09%09%2F*%201%20%26%202%3A%20Allocate%20the%20Node%20%26%20%0A%09%09%09%09Put%20in%20the%20data*%2F%0A%09%09Node%20new_node%20%3D%20new%20Node(new_data)%3B%20%0A%0A%09%09%2F*%203.%20Make%20next%20of%20new%20Node%20as%20head%20*%2F%0A%09%09new_node.next%20%3D%20head%3B%20%0A%0A%09%09%2F*%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%20*%2F%0A%09%09head%20%3D%20new_node%3B%20%0A%09%7D%20%0A%0A%09%2F*%20Function%20to%20print%20linked%20list%20*%2F%0A%09void%20printList()%20%0A%09%7B%20%0A%09%09Node%20temp%20%3D%20head%3B%20%0A%09%09while%20(temp%20!%3D%20null)%20%0A%09%09%7B%20%0A%09%09%09System.out.print(temp.data%2B%22%20%22)%3B%20%0A%09%09%09temp%20%3D%20temp.next%3B%20%0A%09%09%7D%20%0A%09%09System.out.println()%3B%20%0A%09%7D%20%0A%0A%09%2F*%20Drier%20program%20to%20test%20above%20functions%20*%2F%0A%09public%20static%20void%20main(String%20args%5B%5D)%20%0A%09%7B%20%0A%09%09LinkedListExample%20wikitechylist%20%3D%20new%20LinkedListExample()%3B%20%0A%09%09wikitechylist.push(63)%3B%20%0A%09%09wikitechylist.push(50)%3B%20%0A%09%09wikitechylist.push(50)%3B%20%0A%09%09wikitechylist.push(42)%3B%20%0A%09%09wikitechylist.push(27)%3B%20%0A%09%09wikitechylist.push(27)%3B%20%0A%09%09%0A%09%09System.out.println(%22LinkedList%20before%20removal%20of%20duplicates%22)%3B%20%0A%09%09wikitechylist.printList()%3B%20%0A%09%09%0A%09%09wikitechylist.removeDuplicates()%3B%20%0A%09%09%0A%09%09System.out.println(%22LinkedList%20after%20removal%20of%20elements%22)%3B%20%0A%09%09wikitechylist.printList()%3B%20%0A%09%7D%20%0A%7D%20″ message=”” highlight=”” provider=”manual”/]
[pastacode lang=”markdown” manual=”LinkedList%20before%20removal%20of%20duplicates%0A27%2027%2042%2050%2050%2063%20%0ALinkedList%20after%20removal%20of%20elements%0A27%2042%2050%2063″ message=”” highlight=”” provider=”manual”/]

Time Complexity

  • O(n) where n is number of nodes in the given linked list.
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like