LeetCode解题心得,欢迎交流! 第三日
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
if(head == NULL) return NULL;
struct ListNode *dummy=(struct ListNode *)malloc(sizeof(struct ListNode));
dummy->next=head;
struct ListNode *pre=dummy;
struct ListNode *cur=head;
while(cur != NULL)
{
while(cur->next!=NULL && cur->val==cur->next->val)
{
cur=cur->next;
}
if(pre->next == cur)
{
pre=pre->next;
}
else
{
pre->next=cur->next;
}
cur=cur->next;
}
return dummy->next;
}