/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head){
if(head == NULL || head->next == NULL)
return head;
struct ListNode *prev = NULL, *cur = head, *next = head->next;
while(next)
{
if(cur->val == next->val)
{
while(next && next->val == cur->val)//这里要加上next为真的判断,可能next直接往后走到NULL了
{
next = next->next;
}
while(cur != next)//删掉重复的元素
{
struct ListNode* del = cur;
cur = cur->next;
free(del);
}
if(prev == NULL)//单独考虑开头为相同节点的情况
{
head = cur;
}
else
{
prev->next = cur;
}
if(next)
next = next->next;//单独考虑结尾为相同节点的情况
}
else
{
prev = cur;
cur = next;
next = next->next;
}
}
return head;
}