Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
,
return 1->2
.
Given 1->1->2->3->3
,
return 1->2->3
.
C语言代码:
struct ListNode* deleteDuplicates(struct ListNode* head) {
struct ListNode* p;
struct ListNode* q;
if(head==NULL||head->next==NULL)
return head ;
p=head;
q=head->next;
while(q!=NULL)
{
if(p->val==q->val)
{
p->next=q->next;
q=q->next;
}
else
{
p=q;
q=q->next;
}
}
return head;
}