【LeetCode】83.删除排序链表中的重复元素

/**
 * 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) {
        set<int> s;
        ListNode* temp;
        ListNode* p=head;
        ListNode* pre=head;
        while(p!=NULL){
            if(s.find(p->val)!=s.end()){
                pre->next=p->next;
            }else {
                if(p!=head){
                    pre=pre->next;
                }
                s.insert(p->val);
            }
            p=p->next;
        }
        return head;
    }
};

猜你喜欢

转载自www.cnblogs.com/lettleshel/p/9302358.html