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;
}
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