题解:
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/
struct ListNode* deleteDuplicates(struct ListNode* head) {int flag;//标记是否需要删除struct ListNode* dummy=(struct ListNode*)malloc(sizeof(struct ListNode));//创建哑节点dummy->next=head;struct ListNode* Lhead=dummy;struct ListNode* temp=head;//定义三个指向,方便操作while(temp!=NULL&&temp->next!=NULL)/*删除操作具体步骤*/{flag=0;struct ListNode* last=temp->next;while(temp->val==last->val){flag=1;temp=temp->next;last=last->next;if(temp==NULL||last==NULL) break;}if(flag){Lhead->next=last;temp=last;if(last==NULL){return dummy->next;}else last=last->next;}else{Lhead=Lhead->next;temp=temp->next;last=last->next;}}return dummy->next;
}
结果: