public ListNode deleteDuplication(ListNode pHead)
{
if(pHead == null || pHead.next == null)
return pHead;
ListNode newNode = new ListNode(0); //新建结点,防止头节点被删除
newNode.next = pHead;
ListNode tmp = pHead;
ListNode pre = newNode; //保存前一个结点
while(tmp != null && tmp.next != null){
if(tmp.val == tmp.next.val){
int val = tmp.val;
//将跟tmp结点值相等的结点过滤掉
while(tmp != null && tmp.val == val){
tmp = tmp.next;
}
//上一个非重复的结点指向下一个非重复的结点
pre.next = tmp;
}else{
pre = tmp;
tmp = tmp.next;
}
}
return newNode.next;
}

京公网安备 11010502036488号