Remove duplicates from a sorted linked list | C # 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
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node next ; public Node ( ) { data = 0 ; next = null ; } } public class GFG { static Node 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 ; return head_ref ; } static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . next ; } } static void removeDuplicates ( Node head ) { Dictionary < int , bool > track = new Dictionary < int , bool > ( ) ; Node temp = head ; while ( temp != null ) { if ( ! track . ContainsKey ( temp . data ) ) { Console . Write ( temp . data + " ▁ " ) ; track . Add ( temp . data , true ) ; } temp = temp . next ; } } static public void Main ( ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 13 ) ; head = push ( head , 13 ) ; head = push ( head , 11 ) ; head = push ( head , 11 ) ; head = push ( head , 11 ) ; Console . Write ( " Linked ▁ list ▁ before ▁ duplicate ▁ removal ▁ " ) ; printList ( head ) ; Console . Write ( " STRNEWLINE Linked ▁ list ▁ after ▁ duplicate ▁ removal ▁ " ) ; removeDuplicates ( head ) ; } }