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.

Hide Tags

Linked List

 

class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head==NULL) return NULL;
ListNode *lp = head,*rp = head;
while(rp!=NULL){
while(rp!=NULL&&rp->val==lp->val) rp=rp->next;
lp->next = rp;
lp = rp;
}
return head;
}
};
05-21 03:21