/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/*
1,哨兵节点
2,设置curr指向head,prev指向哨兵节点
3,while(curr && curr->next) {
if (curr != curr->next) {
curr和prev一起向后
} else {
while(curr->next && curr->val == curr->next->val) {
curr = curr->next->Next;
}
prev->next = curr->next;
curr = curr->next;
}
}
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* deleteDuplicates(ListNode* head) {
if(!head) {
return head;
}
ListNode *pHead = new ListNode(head->val - 1);
pHead->next = head;
ListNode *prev = pHead, *curr = head, *ret = pHead;
while(curr && curr->next) {
if (curr->val != curr->next->val) {
prev = prev->next;
} else {
while(curr->next && curr->val == curr->next->val) {
curr->next = curr->next->next;
}
prev->next = curr->next;
}
curr = curr->next;
}
return ret->next;
// write code here
}
};