代码: 建议使用虚拟头结点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
typedef struct ListNode lide;
lide*dummy=malloc(sizeof(lide)); //虚拟头结点
dummy->next=head;//初始化虚拟头结点
lide*cur=dummy;
while(cur->next&&cur->next->next)// 条件为且 一个指向空指针 都条件为否
{
lide*temp1=cur->next; //指向第一个数
lide*temp2=cur->next->next; //更新相同数的指向
int flag=1;//作为有相同数的标志
while(temp2&&temp1->val==temp2->val)//若是结尾后面几个都是相同数 判断条件temp2 指向空指针时跳出循环 //或者指向的数不同时 跳出
{
temp2=temp2->next; //更新相同数的指向
flag=0;
}
if(!flag)
cur->next=temp2; //若为0 则有相同数 只需指向此时不同数的节点
else cur=cur->next; //
}
return dummy->next;
}