有争议的无堆错误错误从未分类的链接列表中删除重复项(LeetCode 1836)
问题是要求删除所有值在列表中不是唯一的节点。例如,输入 1-> 2-> 1-> 2-> 3 应该输出 3 只有
解决方案在我的本地IDE和在线上都很好编译器。但是我在Leetcode上遇到了错误。
我的代码将带有错误的测试用例,
================================================================
==31==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000000178 at pc 0x000000374efd bp 0x7ffdccb2bd60 sp 0x7ffdccb2bd58
READ of size 8 at 0x602000000178 thread T0
如果我在deleteNode(listNode*)
中注释delete todelete;
,则
而错误会消失。这是一个问题 https://leetcode.com/problems/删除deplicates-from-an-an-n-Unsorted链接列表/
这是我的解决方案
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
private:
//Delete the next node and return its value
int deleteNode(ListNode* prevToDelete)
{
//Assume prevToDelete has next node
ListNode* toDelete = prevToDelete->next;
prevToDelete->next = toDelete->next;
delete toDelete;
return (prevToDelete->next)?prevToDelete->next->val:-1;
}
public:
ListNode* deleteDuplicatesUnsorted(ListNode* head) {
//Add Dummy Head to the list
ListNode* dummy{new ListNode(0, head)};
ListNode* prev{dummy}, *cur{};
//Map to store value-Node-hit pair
unordered_map<int, pair<ListNode*, bool>> map;
int val{};
while((cur = prev->next)) //Until tail node
{
val = cur->val;
//Duplicate values exist, delete current node
if(map.count(val))
{
deleteNode(prev);
map[val].second = true; //Flag to indicate the node needed to be deleted later
}
else
{
//Add new cur_value-prev_node-hit pair into the map
map.insert({val, {prev, false}});
//Update prev node
prev = cur;
}
}
//Remove remaining nodes hit in the map
for(auto mit = map.begin(); mit != map.end(); mit++)
{
//If it has been hit before
if(mit->second.second)
{
int value = deleteNode(mit->second.first);
//if any node in the map is the oen just deleted, update it
if(map.count(value))
map[value].first = mit->second.first;
}
}
//Update head and delete dummy
head = dummy->next;
delete dummy;
return head;
}
};
The question is asking for deleting all the nodes whose values are not unique in the list. For example, input 1->2->1->2->3 should output 3 only
The solution works fine in both my local IDEs and Online Compilers. But I got the error on LeetCode.
My code is failing the test case with the error
================================================================
==31==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000000178 at pc 0x000000374efd bp 0x7ffdccb2bd60 sp 0x7ffdccb2bd58
READ of size 8 at 0x602000000178 thread T0
If I comment out delete toDelete;
in deleteNode(ListNode*)
, then the error goes away.
Here's the question https://leetcode.com/problems/remove-duplicates-from-an-unsorted-linked-list/
Here is my solution
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
private:
//Delete the next node and return its value
int deleteNode(ListNode* prevToDelete)
{
//Assume prevToDelete has next node
ListNode* toDelete = prevToDelete->next;
prevToDelete->next = toDelete->next;
delete toDelete;
return (prevToDelete->next)?prevToDelete->next->val:-1;
}
public:
ListNode* deleteDuplicatesUnsorted(ListNode* head) {
//Add Dummy Head to the list
ListNode* dummy{new ListNode(0, head)};
ListNode* prev{dummy}, *cur{};
//Map to store value-Node-hit pair
unordered_map<int, pair<ListNode*, bool>> map;
int val{};
while((cur = prev->next)) //Until tail node
{
val = cur->val;
//Duplicate values exist, delete current node
if(map.count(val))
{
deleteNode(prev);
map[val].second = true; //Flag to indicate the node needed to be deleted later
}
else
{
//Add new cur_value-prev_node-hit pair into the map
map.insert({val, {prev, false}});
//Update prev node
prev = cur;
}
}
//Remove remaining nodes hit in the map
for(auto mit = map.begin(); mit != map.end(); mit++)
{
//If it has been hit before
if(mit->second.second)
{
int value = deleteNode(mit->second.first);
//if any node in the map is the oen just deleted, update it
if(map.count(value))
map[value].first = mit->second.first;
}
}
//Update head and delete dummy
head = dummy->next;
delete dummy;
return head;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论