题目描述:给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return 0;
if(!head->next) return head;
int val=head->val;
ListNode*p=head->next;
if(p->val!=val){
head->next=deleteDuplicates(p);
return head;
}else{
while(p&&p->val==val) p=p->next;
return deleteDuplicates(p);
}
}
};