Leetcode 面试题02.01 移除重复节点
class Solution {
public:
ListNode* removeDuplicateNodes(ListNode* head) {
if(head==NULL)
{
return head;
}
ListNode*first=new ListNode(0);
first->next=head;
first=first->next;
ListNode*result=first;
while(first)
{
ListNode*temp=first;
ListNode*mark=new ListNode(first->val);
while(temp)
{
if(temp->next->val==mark->val)
{
ListNode*p=temp->next;
temp->next=temp->next->next;
delete p;
}
temp=temp->next;
}
first=first->next;
}
return result;
}
};
报错:Line 30: Char 32: runtime error: member access within null pointer of type 'ListNode' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:40:32
加入判断head是否为空指针,但还是报错。
通过
ListNode*mark=new ListNode(first->val);
你定义这个节点要干啥?