递归删除- -
/** * 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 == nullptr || head -> next == nullptr) return head; ListNode* next = head -> next; ListNode* tmp; if(head->val == next->val) { while(next && next->val == head -> val) { tmp = next; next = next->next; delete tmp; } delete head; return deleteDuplicates(next); } else { head -> next = deleteDuplicates(next); return head; } } };