1.创建一个新的头结点newhead,在head之前,便于删除开始的重复元素。
提前一步检测后面两个元素是否是一样的。
/**
* 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 *pre=(struct ListNode*)malloc(sizeof(struct ListNode));
pre->next=head;
struct ListNode *newhead=pre;
struct ListNode *p=head;
while(pre->next && p->next)
{
if(pre->next->val == p->next->val)
{
while(p->next && (pre->next->val == p->next->val))
{
p=p->next;
}
pre->next=p->next;
p=p->next;
}
else{
p=p->next;
pre=pre->next;
}
}
return newhead->next;
}