Leetcode 面试题02.01 移除重复节点

问题遇到的现象和发生背景

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);
你定义这个节点要干啥?