题目链接:
https://leetcode-cn.com/problems/remove-duplicate-node-lcci
困难度:简单
编写代码,移除未排序链表中的重复节点。保留最开始出现
的节点。
示例1:
输入:[1, 2, 3, 3, 2, 1]
输出:[1, 2, 3]
示例2:
输入:[1, 1, 1, 1, 2]
输出:[1, 2]
提示:
链表长度在[0, 20000]范围内。
链表元素在[0, 20000]范围内。
进阶:
如果不得使用临时缓冲区,该怎么解决?
这道题目很简单
思路也很清晰
注意: 空间的释放 以下代码为c++ 并未释放内存
java有内存回收机制
空间换时间
unordered_set 查询 重复
/**
* 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==nullptr){
return head;
}
unordered_set<int> unset = {head->val};
ListNode* pos=head;
ListNode* cur;
while(pos->next!=nullptr){
cur = pos->next;
// 若重复
if (unset.count(cur->val)){
pos->next=pos->next->next;
}else{
// 若不重复
unset.insert(cur->val);
pos=pos->next;
}
}
pos->next=nullptr;
return head;
}
};
还有一个就是 题目已经明确了 数的范围
那么 直接设置 bool vis[20001] = {0} 下标对应数字
该方法速度最快 (上一个 unordered_set 查询 插入需要时间)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
private:
bool vis[20001] = {0};
public:
ListNode* removeDuplicateNodes(ListNode* head) {
if(head == NULL || head->next == NULL)
return head;
ListNode* pre = head;
vis[head->val] = true;
ListNode* cur;
while(pre->next!=nullptr){
cur = pre->next;
if(vis[cur->val]){
pre->next = cur->next;
}
else{
vis[cur->val] = true;
pre = cur;
}
}
return head;
}
};
题目中的进阶 只能使用双重循环 暴力破解
时间有点长 不过还能通过
/**
* 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 || head->next == NULL)
return head;
ListNode* cur = head;
ListNode* tmp;
while(cur!=nullptr){
tmp = cur;
while(tmp->next!=nullptr){
if(tmp->next->val==cur->val){
tmp->next=tmp->next->next;
}else{
tmp=tmp->next;
}
}
cur=cur->next;
}
return head;
}
};