Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Sunday, December 31, 2017

Remove Duplicate In LinkedList


Save values to array and check if it already there in array ,remove list

Code:

 Node removeDuplicates(Node head)
    {
        if(head==null)
        return null;
         List<Integer> arr=new ArrayList<Integer>();
         arr.add(head.data);
         Node res=head;
         while(head.next!=null)
         {
             if(arr.contains(head.next.data))
             head.next=head.next.next;
             else
             {arr.add(head.next.data);
             head=head.next;}
         }
         return res;
    }

0 Comments:

Post a Comment

Stats