3.1日刷题记录 remove duplicate node

建造一个hash表,pos指针遍历链表,有不同的就存入hash表,相同的则跳过。题目思路比较简单,活用容器即可。
class Solution { public: ListNode* removeDuplicateNodes(ListNode* head) { if(head==NULL) { return head; } ListNode*pos=head; unordered_set<int>hash={head->val}; while(pos->next!=NULL) { ListNode*cur=pos->next; if(!(hash.count(cur->val))) { hash.insert(cur->val); pos=pos->next; } else { pos->next=pos->next->next; } } return head; } };

浙公网安备 33010602011771号