1957

无聊蛋疼的1957写的低端博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[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;
        }
    }
};