1 删除未排序链表中重复的元素
思路:可用hash表判重,若重复则删除否则就加入hash表中
public class deleteRepeation{
public static void main(String[]args){
Node head=new Node(null);
Node p=head;
int arr[]= {1,6,3,5,7,6,64,2,7,5,3,2,2,12};
for(int i=0;i<arr.length;i++){
p.next=new Node(arr[i]);
p=p.next;
}
remove(head);//删除重复元素
}
public static void remove(Node head){
HashSet set=new HashSet();
Node p1=head;
Node pre=p1;
while(p1!=null){
if(set.contains(p1.value)){//重复则删除
pre.next=p1.next;
}else {//不重复加入set中
set.add(p1.value);
}
pre=p1;
p1=p1.next;
}
}
public static class Node{
Object value;
Node next;
public Node(Object value){
this.value=value;
}
}
}