递归分支的方法进行求解。
ListNode* deleteDuplicates(ListNode* head) {
if(!head||!head->next)return head;
ListNode *p=head->next;
if(head->val==p->val) //当前节点重复转入删除
{
while(head->val==p->val)
{
p=p->next; //删除了当前扫描到的重复元素
if(!p)break;
}
return deleteDuplicates(p); //继续往后遍历
}
head->next=deleteDuplicates(head->next); //当前节点不重复则往后遍历
return head;
}