双指针,flag标记唯一数,cur标记当前位
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return head;
// write code here
ListNode* cur = head;
ListNode* flag = head;
while(cur){
if(flag == cur){//相等时,找下一个
cur = cur->next;
}else if(flag != cur && flag->val != cur->val){//不相等时,移动
flag = cur;
}else if(flag != cur && flag->val == cur->val){//删除节点
flag->next = cur->next;
free(cur);
cur = flag->next;
}
}
return head;
}
};