题解 | #删除链表中重复的结点#

删除链表中重复的结点

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

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    
    ListNode* remove(ListNode *p){
        ListNode* t;
        int val = p->val;
        while(p && p->val == val){
            t = p;
            p = p->next;
            free(t);
        }
        return p;
    }
    
    ListNode* deleteDuplication(ListNode* pHead) {
        if(!pHead || !pHead->next) return pHead;
        ListNode *cur = pHead, *next = NULL, *temp = NULL;
        ListNode *res = new ListNode(0);
        res->next = pHead;
        cur = res;
        while(cur->next){
            if(cur->next && cur->next->next){
                if(cur->next->val == cur->next->next->val){
                    temp = remove(cur->next);
                    cur->next = temp;
                    continue;
                }
            }
            cur = cur->next;
        }
        return res->next;

    }
};
全部评论

相关推荐

09-29 11:19
门头沟学院 Java
点赞 评论 收藏
分享
废铁汽车人:秋招真是牛鬼蛇神齐聚一堂
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务