[leetcode]Remove Duplicates from Sorted List II
Posted on 2014-01-14 18:44 1957 阅读(230) 评论(0) 编辑 收藏 举报递归删除- -
/** * 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; } } };
by 1957