//一、利用哈希表
//T = O(N) S = O(N)
public static Node removeRep1(Node head) {
if(head == null) return head;
HashSet<Integer> set = new HashSet<Integer>();
Node cur = head;
Node pre = null;
while(cur != null) {
if(set.contains(cur.value)) {
pre.next = cur.next;
}else {
set.add(cur.value);
pre = cur;
}
cur = cur.next;
}
return head;
}
//二、根据选择排序原理
//T = O(N^2) S = O(1)
public static Node removeRep2(Node head) {
if(head == null) return head;
Node cur = head;
Node pre = null;
Node next = null;
while(cur != null) {
pre = cur;
next = cur.next;
while(next != null) {
if(next.value == cur.value) {
pre.next = next.next;
}else {
pre = next;
}
next = next.next;
}
cur = cur.next;
}
return head;
}