给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5 输出: 1->2->5
示例 2:
输入: 1->1->1->2->3 输出: 2->3
ok,我们用两个指针 + 递归来写一些。
/**
* 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 NULL;
}else{
if(head&&head->next){
if(head->val==head->next->val){
while(head->next && head->val==head->next->val){
head = head->next;
}
return deleteDuplicates(head->next);
}else{
ListNode* ret = head;
ListNode* next = deleteDuplicates(head->next);
ret->next = next;
return ret;
}
}else{
return head;
}
}
return head;
}
};