题解 | #删除链表中重复的结点#给链表添加头节点后发现操作

删除链表中重复的结点

https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead) {
        ListNode* h = new ListNode(0); //头节点
        h->next = pHead;
        ListNode* cur = h;
        while(cur && cur->next && cur->next->next){
            if(cur->next->val == cur->next->next->val){
                int val = cur->next->val;
                while(cur->next && cur->next->val == val){
                    cur->next = cur->next->next;
                }
            }else{
                cur = cur->next;
            }
        }
        return h->next;
    }
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务