学会思考
刻意练习

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

//时间复杂度O(n),空间复杂度:O(1)
//递归版本
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head) return head;
        ListNode dummy(head->val + 1);
        dummy.next = head;
        recur(&dummy,head);
        return dummy.next;
    }
    private:
        static void recur(ListNode* prev, ListNode *cur){
            if(cur == nullptr)  return;
            if(prev->val == cur->val){
                prev->next == cur->next;
                delete cur;
                recur(prev,prev->next);
            }
            else{
                recur(prev->next,cur->next);
            }
        }
};


//迭代版
class Solution{
    public:
        ListNode* deleteDuplicates(ListNode *head){
            if(head == nullptr) return nullptr;
            
            for(ListNode* prev = head,*cur = head->next; cur; cur = prev->next){
                if(prev->val == cur->val){
                    prev->next = cur->next;
                    delete cur;
                }
                else{
                    prev = cur;
                }
            }
            return head;
        }
};

 

posted on 2019-08-29 23:07  Worty  阅读(127)  评论(0编辑  收藏  举报