Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
1 ListNode *deleteDuplicates(ListNode *head) { 2 // Start typing your C/C++ solution below 3 // DO NOT write int main() function 4 if(head==NULL || head->next==NULL) 5 return head; 6 ListNode * p = head; 7 8 while(p->next!=NULL && p->val == p->next->val){ 9 p=p->next; 10 } 11 12 if(p!=head){ 13 while(head!=p->next) 14 { 15 ListNode * tmp = head; 16 head=head->next; 17 free(tmp); 18 } 19 20 return deleteDuplicates(p->next); 21 } 22 23 head->next=deleteDuplicates(head->next); 24 return head; 25 }