给定一个已排序的链表的头 head
, 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
struct ListNode* deleteDuplicates(struct ListNode* head) {
if(!head||!head->next)
{
return head;
}
struct ListNode* cur=head;
struct ListNode* temp=head;
while(head->next)
{
if(cur->val!=head->val)
{
cur->next=head;
cur=head;
}
head=head->next;
}
if(cur->val!=head->val)
cur->next=head;
else
cur->next=NULL;
return temp;
}