满足进阶时空复杂度OnOn的解法
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
if(pHead==null) return null;
int[] a=new int[1000]; //a数组用于记录每个结点值出现的次数
for(int i=0;i<1000;i++) a[i]=0;
ListNode p=pHead;
while(p!=null){ //遍历一遍链表 统计每个值出现的次数
a[p.val]++;
p=p.next;
}
p=pHead;
ListNode q=pHead.next;
if(q==null) return pHead;
while(q!=null){ //利用p和q在整个链表中删除重复节点
if(a[q.val]>1){
p.next=q.next;
q=p.next;
}
else{
p=p.next;
q=q.next;
}
}
if(a[pHead.val]>1) pHead=pHead.next; //最后判断一下头节点需不需要删
return pHead;
}
}

京公网安备 11010502036488号