删除链表中重复的结点

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        ListNode* pre=new ListNode(0);
        pre->next=pHead;
        ListNode* cur=pHead;//题目中说有重复的结点,那么久至少两个结点
        ListNode* next=pHead->next;
        pHead=pre;
        int tem=-1;
        while(cur!=NULL&&next!=NULL)
        {
            if(cur->val==next->val)
            {
                tem=cur->val;
                pre->next=next->next;
                cur=pre->next;
                while((cur->val==tem)&&cur!=nullptr)
                {
                    pre->next=pre->next->next;
                    cur=pre->next;
                }
                next=cur->next;
            }
            else
            {
                pre=cur;
                cur=next;
                next=cur->next;
            }
        }
        return pHead->next;
    }
};

1.要修改链表的结构,修改的是本结点指针的next。
2.还是不是很清楚链表中关于空指针的报错的问题

全部评论

相关推荐

迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务