/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* deleteDuplicates(ListNode* head) {
// write code here
//特例 为空 直接返回
if(head == NULL || head->next == NULL){
return head;
}
//虚假的表头
ListNode *dummy = new ListNode(0);
dummy->next = head;
//双指针
ListNode *pre = dummy;
ListNode *now = head;
//开始找抄作业的 now以及now->next都不能空
while(now != nullptr && now->next != nullptr){
if(now->val == now->next->val){
int same = now->val;//记录这个值,检查是否有重复
//全是重复的情况 now为空也要考虑
while(now != NULL && now->val == same){
ListNode *temp = now;
now = now->next;
delete temp;
}
pre->next = now;
}
else{
pre = now;
now = now->next;//没找到就都往后走
}
}
ListNode* newHead = dummy->next;
delete dummy;
return newHead;//虚假的表头就不用输出了,只是个幌子,只为了抓抄作业的
}
};