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.
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL) return head;
if(head->next==NULL) return head;
ListNode *p=head;
while(p)
{
ListNode *s=p;
if(p->next!=NULL&&p->next->val==s->val)
{
if(p->next->next!=NULL)
{
s->next=p->next->next;
}
else s->next=NULL;
p=s;
}
else p=p->next;
}
return head;
}
};
Remove Duplicates from Sorted List
原文:http://blog.csdn.net/sinat_24520925/article/details/45477909