//1使用缓冲区N 时间复杂度N
public static void deleteDups(ListNode head) {
Hashtable<Integer,Boolean> table = new Hashtable<Integer,Boolean>();
//当前指针的前一个指针
ListNode pre = null;
while (head != null) {
if (table.contains(head.val)) {
pre.next = head.next;
} else {
table.put(head.val, true);
pre = head;
}
head = head.next;
}
}
//2不使用缓冲区 时间复杂度N*N(直接遍历)
public static void deleteDups2(ListNode head) {
if(head == null || head.next == null) {
return;
}
ListNode current = head;
while(current != null) {
ListNode p = current;
while (p.next != null) {
if (p.next.val == current.val) {
p.next = p.next.next;
} else {
p = p.next;
}
}
current = current.next;
}
}
//3、对链表插入排序(N*logN)