LeetCode -- Remove Duplicates from Sorted List

题目链接

简单题,就是从单链表中删除重复元素。

附上代码:

 1 /**  2  * Definition for singly-linked list.  3  * struct ListNode {  4  * int val;  5  * ListNode *next;  6  * ListNode(int x) : val(x), next(NULL) {}  7  * };  8  */

 9 class Solution { 10 public: 11     ListNode *deleteDuplicates(ListNode *head) { 12         set<int> vis; 13         ListNode *p = head, *pre; 14         while (p != NULL) { 15             if (vis.find(p->val) == vis.end()) { 16                 vis.insert(p->val); 17                 pre = p; 18             } else { 19                 pre->next = p->next; 20  } 21             p = p->next; 22  } 23         

24         return head; 25  } 26 };

 

你可能感兴趣的:(LeetCode)