LeetCode 面试题 02.01. 移除重复节点

(LeetCode 面试题 02.01. 移除重复节点)[https://leetcode-cn.com/problems/remove-duplicate-node-lcci/]

2. Tag

  1. 链表
  2. Code Optimize

3. Code

和数组的处理方式相似!!

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeDuplicateNodes(ListNode* head) {
        if(head==NULL)  return head;
        set<int> se;
        ListNode* p=head;
        se.insert(p->val);
        while(p->next)
        {
            if(se.count(p->next->val)==1)
            {
                p->next=p->next->next;
            }
            else
            {
                se.insert(p->next->val);
                p=p->next;
            }
        }
        return head;
    }
};

猜你喜欢

转载自www.cnblogs.com/HurryXin/p/12904065.html