Remove duplicates from a sorted linked list | CPP program for the above approach ; Function to insert a node at the beginging of the linked * list ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Function to print nodes in a given linked list ; Function to remove duplicates ; Driver Code ; Created linked list will be 11 -> 11 -> 11 -> 13 -> 13 -> 20
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; Node ( ) { data = 0 ; next = NULL ; } } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( Node * node ) { while ( node != NULL ) { cout << node -> data << " ▁ " ; node = node -> next ; } } void removeDuplicates ( Node * head ) { unordered_map < int , bool > track ; Node * temp = head ; while ( temp ) { if ( track . find ( temp -> data ) == track . end ( ) ) { cout << temp -> data << " ▁ " ; } track [ temp -> data ] = true ; temp = temp -> next ; } } int main ( ) { Node * head = NULL ; push ( & head , 20 ) ; push ( & head , 13 ) ; push ( & head , 13 ) ; push ( & head , 11 ) ; push ( & head , 11 ) ; push ( & head , 11 ) ; cout << " Linked ▁ list ▁ before ▁ duplicate ▁ removal ▁ " ; printList ( head ) ; cout << " Linked list after duplicate removal " ; removeDuplicates ( head ) ; return 0 ; }